1. 程式人生 > >Objective-C屬性字串NSAttributedString

Objective-C屬性字串NSAttributedString

NSAttributedString是Objective-C中的屬性字串類,GitHub上也有很多第三方,用得較多的是TTTAttributedLabel,這裡給大家介紹一下系統NSAttributedString類來實現富文字,並可實現點選事件,同時點選事件可攜帶引數。
因為要做點選事件,所以我們用UITextView,首先宣告一個UITextView屬性:

@property (nonatomic, strong) UITextView *textView;

NSAttributedString中NSFontAttributeName是用來設定文字字型的,有很多可以設定的屬性,這裡介紹一些常用的,其他的用到的話修改一下key-value就可以了:

//NSFontAttributeName:文字字型
- (void)setAttributeCorlorAndSize{
    _textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, 100)];
    _textView.editable = NO;
    _textView.selectable = NO;
    _textView.scrollEnabled = NO;
    _textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [self
.view addSubview:_textView]; //初始化屬性字串 NSMutableAttributedString * aAttributedString = [[NSMutableAttributedString alloc] initWithString:@"富文字:文字顏色 字型大小 背景色 下劃線 空心 點選事件"]; //文字顏色 [aAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(4
, 4)]; //文字大小 [aAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange(9, 4)]; //文字背景色 [aAttributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(14, 3)]; //下劃線 NSUnderlineColorAttributeName設定下劃線顏色 [aAttributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:1] range:NSMakeRange(18, 3)]; //空心 [aAttributedString addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithInt:3] range:NSMakeRange(22, 2)]; //點選事件攜帶一個NSInteger型別,值為110的引數 [aAttributedString addAttribute:@"tapID" value:[NSNumber numberWithInteger:110] range:NSMakeRange(25, 4)]; [_textView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(textTapped:)]]; NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init]; //行間距 paragraphStyle.lineSpacing = 15.0; [aAttributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, aAttributedString.length)]; _textView.attributedText = aAttributedString; }

點選事件實現:

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

    NSLayoutManager *layoutManager = _textView.layoutManager;
    CGPoint location = [recognizer locationInView:_textView];
    location.x -= _textView.textContainerInset.left;
    location.y -= _textView.textContainerInset.top;

    NSUInteger characterIndex = [layoutManager characterIndexForPoint:location
                                                      inTextContainer:_textView.textContainer
                             fractionOfDistanceBetweenInsertionPoints:NULL];

    if(characterIndex < _textView.textStorage.length){
        NSRange range;
        id number = [_textView.attributedText attribute:@"tapID" atIndex:characterIndex effectiveRange:&range];
        NSNumber *value_id = number;

        NSLog(@"%@", value_id);
    }
}

NSParagraphStyleAttributeName用來設定段落樣式(字串通過“\n”進行分段,此設定必須在lable.numberOfLines = 0時有效,value通過NSMutableParagraphStyle設定,它有以下屬性)

- (void)setAttributeParagraph{

    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 130, SCREEN_WIDTH, 200)];
    label2.numberOfLines = 0;
    [self.view addSubview:label2];

    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:@"NSParagraphStyleAttributeName 段落樣式(字串通過“\n”進行分段,此設定必須在lable.numberOfLines = 0時有效,value通過NSMutableParagraphStyle設定,它有以下屬性)"];
    //段落樣式
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    //段落間距
    paragraphStyle.paragraphSpacing = 20.0;

    [attributedString addAttribute:NSParagraphStyleAttributeName
                                          value:paragraphStyle
                                          range:NSMakeRange(0, attributedString.length)];

         label2.attributedText = attributedString;
}

效果圖,點選事件就不演示了,但點選“點選事件”四個字的時候,控制檯列印所攜帶的引數110。
這裡寫圖片描述