NSAttributedString能否设置文字下划线?是否支持line break?

#import <CoreText/CoreText.h>
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic, weak) IBOutlet UILabel *label1;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"Some String"];
    [attString addAttribute:(NSString*)kCTUnderlineStyleAttributeName value:[NSNumber numberWithInt:kCTUnderlineStyleSingle] range:(NSRange){0,[attString length]}];
self.label1.attributedText = attString;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end


由于iOS7新出的NSTextStorge是NSMutableAttributedString的子类,所以要用好NSTextStorage,首先要学好NSMutableAttributedString和NSAttributedString。


按个人的理解,NSAttributedString是一个带有属性的字符串,通过该类可以灵活地操作和呈现多种样式的文字数据。


因为是初步使用,所以基本上都是对照着文档上的指导和介绍的方法来写Demo。


首先是两种类的初始化方法(基本上是相似的):


 


  1. 复制代码
     1 // initWithString:  
     2 NSAttributedString *attributedString_str = [[NSAttributedString alloc] initWithString:@"attributedString"];  
     3 NSLog(@"%@", attributedString_str);  
     4 // textView.attributedText = attributedString_str;  
     5   
     6   
     7 // initWithAttributedString:  
     8 NSAttributedString *attributedString_atts = [[NSAttributedString alloc] initWithAttributedString:attributedString_str];  
     9 NSLog(@"%@", attributedString_atts);  
    10 // textView.attributedText = attributedString_atts;  
    11   
    12   
    13 // initWithString:attributes:  
    14 UIColor *backgroundColor = [UIColor blackColor];  
    15 NSNumber *baseLineOffset = [NSNumber numberWithFloat:20.0];  
    16 UIColor *foregroundColor = [UIColor whiteColor];  
    17 NSNumber *kern = [NSNumber numberWithFloat:5.0];  
    18 NSNumber *ligature = [NSNumber numberWithFloat:3.0];  
    19 NSURL *linkURL = [NSURL URLWithString:@"http://www.baidu.com"];  
    20 NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];  
    21 NSDictionary *attrsDic = @{NSForegroundColorAttributeName: foregroundColor,  
    22                            NSBackgroundColorAttributeName: backgroundColor,  
    23                            NSBaselineOffsetAttributeName: baseLineOffset,  
    24                            NSKernAttributeName: kern,  
    25                            NSLigatureAttributeName: ligature,  
    26                            NSLinkAttributeName: linkURL,  
    27                            NSUnderlineStyleAttributeName: underline  
    28                            };  
    29 NSAttributedString *attributedString_str_atts = [[NSAttributedString alloc] initWithString:@"http://www.baidu.com" attributes:attrsDic];  
    30 NSLog(@"%@", attributedString_str_atts);  
    31 // textView.attributedText = attributedString_str_atts;  
    32   
    33   
    34 // initWithFileURL:options:documentAttributes:error:  
    35 NSURL *fileURL = nil;  
    36 fileURL = [[NSBundle mainBundle] URLForResource:@"Dynamic Coloring" withExtension:@"rtf"];  
    37 NSAttributedString *attributedString_fileURL = [[NSAttributedString alloc] initWithFileURL:fileURL options:@{} documentAttributes:nil error:nil];  
    38 NSLog(@"%@", attributedString_fileURL);  
    39 // textView.attributedText = attributedString_fileURL;  
    40   
    41   
    42 // initWithData:options:documentAttributes:error:  
    43 fileURL = nil;  
    44 fileURL = [[NSBundle mainBundle] URLForResource:@"View Layout" withExtension:@"rtf"];  
    45 NSData *data = [[NSData alloc] initWithContentsOfURL:fileURL];  
    46 NSAttributedString *attributedString_data = [[NSAttributedString alloc] initWithData:data options:@{} documentAttributes:nil error:nil];  
    47 NSLog(@"%@", attributedString_data);  
    48 // textView.attributedText = attributedString_data;  
    49   
    50   
    51 // initWithAttributedString:  
    52 NSMutableAttributedString *mutableAttributedString_attrs = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString_fileURL];  
    复制代码

     


非常简单。


 


 


由于NSAttributedString的属性以字典的形式记录,所以要弄清楚其中一些属性对应的键值:



说说几个自己使用上或者了解作用的。


NSBackgroundColorAttributeName:文字背景的颜色。


NSBaselineOffsetAttributeName:设置行距。


NSFontAttributeName:字体的样式,必须设置NSFont作为Value,NSFont在iOS中不能使用,这个属性的设置上我还不会。


NSForegroundColorAttributeName:文字颜色。


NSUnderlineStyleAttributeName:为文字添加下划线。必须设置NSNumber对象为Value。如:


 


  1. NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];  

 


以上attribute在NSAttributedString和NSMutableAttributedString对象创建时可以设定,不同的是NSAttributedString对象在创建成功后其属性便不可改变,而NSMutableAttributedString的属性是可以改变的。如下所示:


 


  1. 复制代码
     1 // Change NSMutableAttributedString  
     2 [mutableAttributedString_attrs beginEditing];  
     3 /* 
     4 // addAttributes:range: 
     5 [mutableAttributedString_attrs addAttributes:@{NSLinkAttributeName: @"www.baidu.com", 
     6                                                NSBackgroundColorAttributeName: [UIColor greenColor], 
     7                                                NSUnderlineStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleDouble] 
     8                                                } 
     9                                        range:NSMakeRange(0, [attributedString_fileURL length])]; */  
    10 // addAttribute:value:range:  
    11 [mutableAttributedString_attrs addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleThick] range:NSMakeRange(0, 10)];  
    12 [mutableAttributedString_attrs addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithFloat:20.0] range:NSMakeRange(20, 100)];  
    13 [mutableAttributedString_attrs endEditing];  
    14 NSLog(@"%@", mutableAttributedString_attrs);  
    15  
    复制代码

     


在修改文字属性的开头和结尾要分别调用beginEditing和endEditing方法,这些方法可以在文字属性发生变化时发送消息给事件监听者。


可以为某个范围内的文字单个地添加属性key-value对,也可以添加一个属性字典。
注意到在控制台输出NSAttributedString或NSMutableAttributedString时,输出的不仅是字符内容,还有对应的属性值:


 


  1. 复制代码
    1 2013-08-11 16:40:44.737 AttributedString_i7_Demo[43468:a0b] http://www.baidu.com{  
    2     NSBackgroundColor = "UIDeviceWhiteColorSpace 0 1";  
    3     NSBaselineOffset = 20;  
    4     NSColor = "UIDeviceWhiteColorSpace 1 1";  
    5     NSKern = 5;  
    6     NSLigature = 3;  
    7     NSLink = "http://www.baidu.com";  
    8     NSUnderline = 1;  
    9 }  
    复制代码

     


 


 


也可以获取某一段文字的属性:


 


  1. 1 // get attribute  
    2 NSRange range = NSMakeRange(0, mutableAttributedString_attrs.length);  
    3 // attributesAtIndex:effectiveRange:  
    4 NSDictionary *dic = [mutableAttributedString_attrs attributesAtIndex:0 effectiveRange:&range];  
    5 NSLog(@"%@", [dic objectForKey:NSFontAttributeName]);  

     



如果要将NSMutableAttributedString对象赋值给NSAttributedString时,要使用copy方法:


  1. 1 textView.attributedText = [mutableAttributedString_attrs copy]; 

    以上是对NSAttributedString和NSMutableAttributedString最基本的使用(主要还是NSAttributedString),为了强化作为NSMutableAttributedString的子类NSTextStorage处理文本的能力,明显在这两个类中增加了许多新的成员,如NSTextAttachment,在这里没有用上,必须继续深入学习。

     
 

设置UITextView,UILabel 中的文字加下划线


 


1
2
3
4
5
6
7
8
9
10
11
//添加下划线
-(NSAttributedString*) getAttributedString:(NSAttributedString*) attributedString isUnderline:(BOOL) isUnderline
{
    NSNumber *valuUnderline = [NSNumbernumberWithBool:isUnderline];
    NSRange rangeAll = NSMakeRange(0, attributedString.string.length);
    NSMutableAttributedString *as = [attributedString mutableCopy];
    [as beginEditing];
    [as addAttribute:NSUnderlineStyleAttributeNamevalue:valuUnderline range:rangeAll];
    [as endEditing];
    return as;
}

 


使用


1
2
textView.attributedText = [self getAttributedString:_mainTextView.attributedText isUnderline:YES];
label.attributedText =  [self getAttributedString:_mainTextView.attributedText isUnderline:YES];

转载于:https://www.cnblogs.com/lgphp/p/4109632.html

<think>好的,我现在需要帮助用户解决如何在网页中去掉超链接的下划线的问题。用户之前询问过如何改变链接颜色为金色,现在的新问题是关于去除下划线。我需要结合之前的回答和用户提供的引用内容来给出准确的方法。 首先,用户提到了两个引用,引用[1]和引用[2],都涉及使用CSS的text-decoration属性来去掉下划线。特别是引用[1]指出,如果只设置a标签的text-decoration为none,可以去除下划线,并且在悬停时也不会显示。引用[2]给出了一个具体的例子,使用类选择器来应用这个样式。这些信息验证了使用text-decoration: none的正确性。 回顾用户之前的提问,我之前给出的回答中确实提到了在限定作用范围的部分,通过类选择器.gold-links a来设置color和text-decoration: none。所以用户可能已经看过这部分,但需要更详细的指导。 用户现在的需求是消除超链接的下划线,可能在HTML或CSS中实现。我需要分情况说明,包括全局和局部的方法,以及如何处理不同状态下的下划线,比如默认状态、悬停、访问后等。同时,用户可能希望知道如何确保所有状态下都没有下划线,而不仅仅是默认状态。 考虑到用户之前的问题涉及颜色修改,我需要保持回答的一致性,可能提到同时修改颜色和下划线的情况。另外,用户可能对优先级控制或CSS变量感兴趣,但当前问题更基础,应优先解决核心需求。 需要确保回答结构清晰,步骤详细,并提供代码示例。同时,注意用户提供的引用中的信息,确认text-decoration的正确用法,并强调需要覆盖所有伪类选择器(如:hover, :visited等)来彻底去除下划线。 另外,用户可能对为什么有时候下划线仍然存在有疑问,比如未正确覆盖所有状态或优先级问题,因此需要提及这些注意事项,并建议使用!important或更具体的选择器来覆盖现有样式。 最后,生成的相关问题应该与当前主题相关,比如如何添加悬停效果、不同状态样式管理,或者如何结合颜色和下划线样式修改,这样能帮助用户进一步学习相关知识。</think>在网页设计中去除超链接下划线,主要通过CSS的`text-decoration`属性实现。以下是具体实现方法,结合不同场景和需求: ### 一、全局去除所有链接下划线(基础方法) ```css a { text-decoration: none; /* 移除默认状态的下划线 */ } /* 必须覆盖所有伪类状态 */ a:visited, a:hover, a:active { text-decoration: none; /* 确保悬停/点击时也不显示 */ } ``` ⚠️ 常见错误:若仅设置`a{...}`而未覆盖伪类,悬停时可能恢复默认样式[^1] ### 二、局部去除特定链接下划线 ```html <style> .no-underline { text-decoration: none !important; /* 强制覆盖其他样式 */ } </style> <a href="#" class="no-underline">无下划线链接</a> ``` ### 三、结合颜色修改的完整示例 ```css a.gold-link { color: #FFD700; /* 金色 */ text-decoration: none; /* 去下划线 */ transition: all 0.3s; /* 添加过渡动画 */ } a.gold-link:hover { color: #FFE55C; /* 悬停时更亮的金色 */ text-decoration: underline; /* 可选择性恢复下划线 */ } ``` ### 四、嵌套选择器精准控制 ```css /* 仅修改导航栏内的链接 */ nav.main-nav a { text-decoration: none; } /* 保留文章正文链接下划线 */ article.content a { text-decoration: underline; } ``` ### 五、动态效果进阶方案 ```css a { position: relative; text-decoration: none; } a::after { /* 创建自定义下划线 */ content: ''; position: absolute; width: 0; height: 2px; bottom: -3px; left: 0; background: gold; transition: width 0.3s; } a:hover::after { width: 100%; /* 悬停时展开金色下划线 */ } ``` [^1]: 根据CSS规范,浏览器对链接状态的默认样式有不同实现,需显式覆盖所有伪类 [^2]: 复杂项目建议采用CSS预处理器维护链接样式 --相关问题-- 1. 如何为不同状态的链接(悬停/点击)创建渐变下划线动画? 2. CSS中`!important`规则的使用场景和注意事项有哪些? 3. 如何通过SCSS/Less更高效地管理链接样式?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值