2014-12-05 79 views

回答

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

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];