iOS是否支持可跨越的字符串?

时间:2014-12-05 11:47:14

标签: ios objective-c

iOS中是否支持spannable字符串?
我想创建一个underlineSpan并单击它然后打开一些其他视图控制器。

2 个答案:

答案 0 :(得分:3)

NSAttributedString *title;
title = [[NSAttributedString alloc] initWithString:@"hello how r u..." attributes:@{ NSFontAttributeName : [UIFont fontWithName:@"Noteworthy-Bold" size:15], NSUnderlineStyleAttributeName : @1 , NSStrokeColorAttributeName : [UIColor blackColor]}]; //1
UILabel *label;

label = [[UILabel alloc] initWithFrame:CGRectMake( (self.view.bounds.size.width - title.size.width) / 2.0f, 40.0f, title.size.width, title.size.height)]; //2

label.attributedText = title; //3
[self.view addSubview:label]; 

答案 1 :(得分:2)

是的,它们在iOS中被称为NSAttributedStrings。

添加下划线的示例代码

NSString *str = @"Amit";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];

[attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(0, 4)];

更多信息@ Apple Documentation

要添加下划线的链接,请查看以下代码:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Google Webpage"];
[attributedString addAttribute:NSLinkAttributeName
                         value:@"http://www.google.com"
                         range:[[attributedString string] rangeOfString:@"Google Webpage"]];


NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor],
                                 NSUnderlineColorAttributeName: [UIColor lightGrayColor],
                                 NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};

// assume that textView is a UITextView previously created (either by code or Interface Builder)
textView.linkTextAttributes = linkAttributes; // customizes the appearance of links
textView.attributedText = attributedString;
textView.delegate = self;

此源代码已在@ Raywenderlich website

中找到