常用的是其子类:NSMutableAttributedString 加中划线,删除线的方法:
<pre name="code" class="objc">//获取要改变的字符串
NSString *old_str = [NSString stringWithFormat:@"¥%d",_deal.listPrice/100];
//创建一个 NSMutableAttributedString对象
NSMutableAttributedString *old_attriStr = [[NSMutableAttributedString alloc] initWithString:old_str];
//给指定NSRange的字符串添加属性
[old_attriStr addAttributes:@{NSStrikethroughStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle]} range:NSMakeRange(0, old_attriStr.length)];
self.oldPriceLabel.attributedText = old_attriStr;
现在你可以在任意的UIKit对象上使用NSAttributedString 了,比如说是一个UILabel或是一个UITextField,见以下代码:
- #import <SSKeychain.h>
-
- - (BOOL)saveCredentials:(NSError **)error {
- SSKeychainQuery *query = [[SSKeychainQuery alloc] init];
- query.password = @"MySecretPassword";
- query.service = @"MyAwesomeService";
- query.account = @"John Doe";
- query.synchronizable = YES;
- return [query save:&error];
- }
-
- - (NSString *)savedPassword:(NSError **)error {
- SSKeychainQuery *query = [[SSKeychainQuery alloc] init];
- query.service = @"MyAwesomeService";
- query.account = @"John Doe";
- query.synchronizable = YES;
- query.password = nil;
- if ([query fetch:&error]) {
- return query.password;
- }
- return nil;
注意:NSHTMLTextDocumentType 只是NSDocumentTypeDocumentAttribute key一种可能的值。你还可以使用NSPlainTextDocumentType,NSRTFTextDocumentType或是NSRTFDTextDocumentType。
你还可以从NSAttributedString中创建一个HTML字符串,如下:
- NSAttributedString *attrString; // from previous code
- NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
-
- NSData *htmlData = [attrString dataFromRange:NSMakeRange(0, [attrString length]) documentAttributes:options error:nil];
- NSString *htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)