属性字符串(NSAttributedString)的简单应用

属性字符串NSAttributedString 可以对字符串附加格式信息,由于对于对不同文本片段使用不同的格式,属性字符串类特别合适。

IOS 6中对样式文本有了大改善,大部分主要的UIKit控件都允许使用属性字符串。举例UILable,只要创建一个属性字符串,然后赋值给标签的attributedText属性即可。

- (IBAction)buttonPressed:(UIButton *)sender {
    //按钮标题
    NSString *title = [sender titleForState:UIControlStateNormal];
    //标签标题
    NSString *plainTitle = [NSString stringWithFormat:@"%@ button press", title];
//    _statusLabel.text = plainTitle;
    
    //创建属性字符串对象
    NSMutableAttributedString *styledText = [[NSMutableAttributedString alloc] initWithString:plainTitle];
    
    //修改属性字符串中的某文件的字体(加粗)
    NSDictionary *attributes =
    @{
      NSFontAttributeName : [UIFont boldSystemFontOfSize:_statusLabel.font.pointSize]
      };
    NSRange rang = [plainTitle rangeOfString:title];
    [styledText setAttributes:attributes range:rang];
    
    //赋值给标签
    _statusLabel.attributedText = styledText;
}

 

效果如下:

Left button press

 

转载于:https://www.cnblogs.com/smile-smile/p/5297378.html

1788445834680.apk 立即下载

相关推荐

运动员运动表现纵向数据集

纵向运动员运动表现数据集包含250名运动员10000条训练会话重复观测记录,涵盖篮球、排球、足球、跑步、游泳和无损伤跑步者六个运动类别。包括速度、加速度、反应时间、准确性、耐力、力量、敏捷性等表现变量,以及训练负荷、疲劳、恢复和训练成效指标。用于研究和实验分析。

NSAttributedString的用法

@interface NSAttributedString : NSObject NSCopying, NSMutableCopying, NSCoding> - (NSString *)string; - (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)

树上的男爵 1万+

MATLAB中读写Zarr文件的接口。Zarr是一种用于科学数据的文件格式(类似于HDF5和netCDF),在云存储和并行计算环境中进行了优化.rar

MATLAB中读写Zarr文件的接口。Zarr是一种用于科学数据的文件格式(类似于HDF5和netCDF),在云存储和并行计算环境中进行了优化.rar

iOS NSAttributedString属性字符串

ui

邓俊杰 6804

iOS 正则处理 NSString,NSAttributedString

处理字符串  NSString * regExpStr = @"(\\d{3})\\d{4}(\\d{0,4})"; NSString *resultString = [self stringByReplacingOccurrencesOfRegex:regExpStr withString:@"$1****$2"]; return resultString; 处理属性字符串

颜忠贤 JoesRain 602

使用富文本OHAttributedLabel

OHAttributedLabel富文本标签 https://github.com/AliSoftware/OHAttributedLabel 以下是我渲染出来的效果 OHAttributedLabel This class allows you to use a UILabel with NSAttributedStrings, in order to display styl...

weixin_30840253的博客 83

NSAttributedString 详解(属性字符串)

NSAttributedString可以让我们使一个字符串显示的多样化,但是目前到iOS5为止,好像对它支持的不是很好,因为显示起来不太方便(至少没有在OS X上方便)。 首先导入CoreText.framework,并在需要使用的文件中导入: #import 创建一个NSMutableAttributedString: NSMutableAttributedStrin

Braver_Smile的专栏 1562

NSAttributedString绘制多色镂空字符串

第一步 设计接口(这是一个很好的习惯,由接口驱动编程) 创建一个UIView的子类DrawStringView 然后,这个类的接口有两个 1 显示的String内容 2 初始化的方法 这样,DrawStringView.h的完整代码如下

Leo的专栏 2096

字符属性NSAttributedString

An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. 这

Vic 472

属性字符串 NSMutableAttributedString/NSAttributedString

由于iOS7新出的NSTextStorge是NSMutableAttributedString的子类,所以要用好NSTextStorage,首先要学好NSMutableAttributedString和NSAttributedString。 按个人的理解,NSAttributedString是一个带有属性字符串,通过该类可以灵活地操作和呈现多种样式的文字数据。   ali

岽少的专栏 2039

Foundation框架 —— NSAttributedString

#import #import NS_CLASS_AVAILABLE(10_0, 3_2) @interface NSAttributedString : NSObject NSCopying, NSMutableCopying, NSSecureCoding> @property (readonly, copy) NSString *s

u014221412的专栏 762

NSAttributedString 详解

NSAttributedString可以让我们使一个字符串显示的多样化,但是目前到iOS 5为止,好像对它支持的不是很好,因为显示起来不太方便(至少没有在OS X上方便)。 首先导入CoreText.framework,并在需要使用的文件中导入: #import 创建一个NSMutableAttributedString: NSMutableAttributedString *attr

Bannings的专栏 6万+

NSAttributedString用法简介,控制文字颜色,背景色等属性

NSAttributedString用来管理字符串以及和字符串相关的属性。 直接上代码了。 UIFont *font = [UIFont systemFontOfSize:14.0]; NSDictionary *dict = [NSDictionary dictionaryWithObject:font

u012678162的专栏 4409

双立柱油脂加注机.rar

双立柱油脂加注机.rar

食品包装礼品包装设备.rar

食品包装礼品包装设备.rar

全自动iphone贴膜机3.rar

全自动iphone贴膜机3.rar

曲轴箱加工专机.rar

曲轴箱加工专机.rar

YOLO算法人像特写场景手势目标检测数据集-106张-包含 VOC 和 Yolo 格式标签-支持多种算法训练模型.zip

下拉可见数据集可视化效果示意。 YOLO人像特写场景手势目标检测数据集 目标类别:['peace', 'shaka', 'thumbsup'] 中文类别:['和平手势', '夏威夷手势', '竖起大拇指'] 训练集:79 张 验证集:16 张 测试集:11 张 总计:106 张 该数据集提供了data.yaml文件,内容如下: train: ../train/images val: ../valid/images test: ../test/images nc: 3 names: ['peace', 'shaka', 'thumbsup']

上一篇: ARC下OC对象和CF对象之间的桥接(bridge)
下一篇: xcode常用的快捷键
dcpe8917
博客等级 码龄12年 1粉丝 0原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值