1. 程式人生 > >UILabel設定屬性字串,可以增加點選事件點選

UILabel設定屬性字串,可以增加點選事件點選

1.首先啊,label可以設定屬性字串,設定不同的字型顏色,字型大小,但是要增加點選事件,不好操作。

2.所以文章是設定textView來完成的。

- (UITextView *)textView {
    if (!_textView) {
        _textView = [[UITextView alloc] init];
//        NSMutableAttributedString *attributedString = [NSString attributuStringWithString:self.descText lineSpacing:10];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"1.您可能很久未更新最新資料導致圖表有誤,請及時重新整理;\n以上分析根據公積金官網資料統計分析所得,可能存在有誤。"];
        [attributedString addAttribute:NSLinkAttributeName
                                 value:@"ProvidentFundshuaxin://"
                                 range:[[attributedString string] rangeOfString:@"重新整理"]];
        [attributedString addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13]} range:[self.descText rangeOfString:@"重新整理"]];
        _textView.attributedText = attributedString;
        _textView.linkTextAttributes = @{NSForegroundColorAttributeName: ColorFromHexRGB(0xe73c00),
                                         NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
        
        _textView.textColor = ColorFromHexRGB(0xa0a0a0);
        _textView.delegate = self;
        _textView.editable = NO;        //必須禁止輸入,否則點選將彈出輸入鍵盤
        _textView.scrollEnabled = NO;
        
//        NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
//        paragraphStyle.lineSpacing = 20;// 字型的行間距
//        NSDictionary *attributes = @{
//                                     NSFontAttributeName:[UIFont systemFontOfSize:12],
//                                     NSParagraphStyleAttributeName:paragraphStyle
//                                     };
//        _textView.typingAttributes = attributes;
//                        _textView.layer.borderColor = [UIColor cyanColor].CGColor;
//                        _textView.layer.borderWidth = 1.0f;
    }
    return _textView;
}
textView代理事件
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
    NSRange range = [@"1.您可能很久未更新最新資料導致圖表有誤,請及時重新整理;\n以上分析根據公積金官網資料統計分析所得,可能存在有誤。" rangeOfString:@"重新整理"];
    if (NSEqualRanges(characterRange, range)) {
        NSLog(@"設定您的自定義事件");
//        if (self.refreshAccountBlock) {
//            self.refreshAccountBlock();
//        }
    }
    return YES;
}

ok 完美解決。