【问题标题】:How do I make a particular word in a UILabel selectable (Swift 2.0)?如何在 UILabel 中选择特定单词(Swift 2.0)?
【发布时间】:2015-12-29 14:58:04
【问题描述】:

我一整天都面临一个大问题,可能有一个简单的解决方案。我有一个UILabel,其中包含一个NSAttributedString。我希望UILabel 中的特定范围可以点击。我不想要任何第三方功能。

这是我的示例代码:

let str = dict["itemName"] as! NSString
range = string.rangeOfString(dict["itemName"] as! String)!
index = string.startIndex.distanceTo(range.startIndex)
attributedString.addAttribute(NSForegroundColorAttributeName, value: color, range: NSRange(location: index, length: str.length))

【问题讨论】:

  • 你能举个例子吗?

标签: ios swift uilabel nsattributedstring


【解决方案1】:

您可以使用 UITextview 并为其添加点击手势,而不是使用 UILabel。这是一个例子,你可以达到同样的效果。

/* Set Tap gesture on Text view */

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapResponse:)];
tapGesture.numberOfTapsRequired = 1;
[yourTextView addGestureRecognizer:tapGesture];

- (void) tapResponse:(UITapGestureRecognizer *)recognizer
{

UITextView *textView =  (UITextView *)recognizer.view;
CGPoint location = [recognizer locationInView:textView];

CGPoint position = CGPointMake(location.x, location.y);

//get location in text from textposition at point
UITextPosition *tapPosition = [textView closestPositionToPoint:position];

//fetch the word at this position (or nil, if not available)
UITextRange *textRange = [textView.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
NSString *tappedWord = [textView textInRange:textRange];

NSLog(@"tapped word : %@", tappedWord);    
}

【讨论】:

  • 但我在 uitableview 单元格中填充值
【解决方案2】:
**ClickableTextView.h**

@protocol ClickableTextViewDelegate <NSObject>

@required
-(void)textview:(UITextView *)clickableTextView clickedText:(NSString *)text;

@end

@interface ClickableTextView : UITextView

@property (weak, nonatomic) id <ClickableTextViewDelegate> tapDelegate;

-(instancetype)initWithDelegate:(id)delegate;

@end


**ClickableTextView.m**

-(void)awakeFromNib {
    [super awakeFromNib];

    [self setProperties];
}


-(void)setProperties {

  self.userInteractionEnabled = YES;
  self.editable = NO;
  self.scrollEnabled = NO;
  self.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
  self.dataDetectorTypes = UIDataDetectorTypeLink;
  self.backgroundColor = [UIColor clearColor];

  /* Set Tap gesture on Text view */
  UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapResponse:)];
  tapGesture.numberOfTapsRequired = 1;
  [self addGestureRecognizer:tapGesture];

}

- (void) tapResponse:(UITapGestureRecognizer *)recognizer
{

UITextView *textView =  (UITextView *)recognizer.view;
CGPoint location = [recognizer locationInView:textView];

CGPoint position = CGPointMake(location.x, location.y);

//get location in text from textposition at point
UITextPosition *tapPosition = [textView closestPositionToPoint:position];

//fetch the word at this position (or nil, if not available)
UITextRange *textRange = [textView.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
NSString *tappedWord = [textView textInRange:textRange];

NSLog(@"tapped word : %@", tappedWord); 

if(tappedWord) {

    if([self.tapDelegate respondsToSelector:@selector(textview:clickedText:)]){
        [self.tapDelegate textview:self clickedText:tappedWord];
    }
 }

}

在你的 TableViewCell 中使用这个自定义类而不是 UITextView,然后在你的

中设置委托
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

并在委托方法中获取结果。

-(void)textview:(UITextView *)clickableTextView clickedText:(NSString *)text {
}

【讨论】:

    【解决方案3】:

    Swift 2.0:

    最好使用 UITextView。

    1 ) 制作 TapGesture 并添加到 UITextView。 覆盖 func viewDidLoad() { super.viewDidLoad()

        let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapResponse:")
        tapGesture.numberOfTapsRequired = 1
        sampleTextView.addGestureRecognizer(tapGesture)    
    }
    

    2) 添加获取选中单词的方法。

    func tapResponse(recognizer: UITapGestureRecognizer) {
            let location: CGPoint = recognizer.locationInView(sampleTextView)
            let position: CGPoint = CGPointMake(location.x, location.y)
            let tapPosition: UITextPosition = sampleTextView.closestPositionToPoint(position)!
            let textRange: UITextRange = sampleTextView.tokenizer.rangeEnclosingPosition(tapPosition, withGranularity: .Word, inDirection: 1)!
    
            let tappedWord: String = sampleTextView.textInRange(textRange)!
            print("tapped word : %@", tappedWord)
        }
    

    【讨论】:

    • 要求 UILabel,而不是 UITextView
    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 2018-08-10
    • 2021-01-15
    • 1970-01-01
    相关资源
    最近更新 更多