NSAttributedString绘制多色镂空字符串
来源:互联网 发布:淘宝市场需求分析 编辑:程序博客网 时间:2024/05/08 21:27
原创Blog,转载请注明出处
http://blog.csdn.net/hello_hwc?viewmode=contents
NSAttributeString顾名思义,就是可以携带各种属性的字符穿,提供了一种管理字符串中单个字符属性(字体,颜色,下划线等等)属性的方式。
继承自NSObject,所以其不具有UIKit的特性。
首先看下目标效果图
然后
第一步 设计接口(这是一个很好的习惯,由接口驱动编程)
创建一个UIView的子类DrawStringView
然后,这个类的接口有两个
1 显示的String内容
2 初始化的方法
这样,DrawStringView.h的完整代码如下
#import <UIKit/UIKit.h>@interface DrawStringView : UIView@property (strong,nonatomic)NSString * contentString;-(id)initWithContent:(NSString *)content Frame:(CGRect)frame;@end第二步 完成实现
具体的过程见代码注释
这样,完整代码如下
</pre><pre name="code" class="objc">//// DrawStringView.m// ProjectForCSDN//// Created by huangwenchen on 14/12/25.// Copyright (c) 2014年 huangwenchen. All rights reserved.//#import "DrawStringView.h"@implementation DrawStringView#pragma mark - property//每次修改contentString,都应该重新绘制-(void)setContentString:(NSString *)contentString{ _contentString = contentString; [self setNeedsDisplay];//重新绘制,调用drawRect}#pragma mark - redraw-(void)drawRect:(CGRect)rect{ NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init]; style.alignment = NSTextAlignmentCenter;//居中 style.lineBreakMode = NSLineBreakByWordWrapping;//以词为单位换行 UIFont * font = [UIFont fontWithName:@"Helvetica" size:30]; NSMutableAttributedString * attributeStirng = [[NSMutableAttributedString alloc]initWithString:self.contentString]; NSDictionary * firstPartAttributes = @{ NSFontAttributeName:font,// 字体 NSParagraphStyleAttributeName:style,//绘制样式 NSForegroundColorAttributeName:[UIColor whiteColor],//填充色 NSStrokeColorAttributeName:[UIColor blueColor],//描边色 NSStrokeWidthAttributeName:@(3)//描边线宽 }; NSDictionary * secondPartAttributes = @{ NSFontAttributeName:font, NSParagraphStyleAttributeName:style, NSForegroundColorAttributeName:[UIColor whiteColor], NSStrokeColorAttributeName:[UIColor redColor], NSStrokeWidthAttributeName:@(3) }; NSUInteger halfLocation = (int)attributeStirng.length/2; [attributeStirng addAttributes:firstPartAttributes range:NSMakeRange(0,halfLocation)];//前一半的样式 [attributeStirng addAttributes:secondPartAttributes range:NSMakeRange(halfLocation,attributeStirng.length - halfLocation)];//后一半的样式 [attributeStirng drawInRect:self.bounds];}#pragma mark - init method-(id)initWithContent:(NSString *)content Frame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { self.contentString = content; self.opaque = NO;//不透明 } return self;}@end当然,NSAttributeString还有更多的属性,以上是常用的几种,更多的属性参照XCode中的文档,很详细
三 使用这个类
UIView * viewWithString = [[DrawStringView alloc] initWithContent:@"This is test string" Frame:CGRectMake(100, 100, 200, 200)];[self.view addSubview:viewWithString];
2 0
- NSAttributedString绘制多色镂空字符串
- NSAttributedString绘制多色镂空字符串
- NSAttributedString 绘制
- 绘制多色窗口
- NM_CUSTOMDRAW消息相应绘制多色列表框
- NSAttributedString 详解(属性字符串)
- NSAttributedString字符串属性
- iOS-属性字符串NSAttributedString
- wpf绘制圆环,实现图片镂空
- iOS NSAttributedString(属性字符串)
- 属性字符串NSAttributedString简易教程
- Objective-C属性字符串NSAttributedString
- NSAttributedString
- NSAttributedString
- NSAttributedString
- NSAttributedString
- NSAttributedString
- NSAttributedString
- OnGlobalLayoutListener获得一个视图的高度
- cacti
- mysql默认值的问题
- Java把double数据写入文件中
- ArgumentError:Error #2004:某个参数无效
- NSAttributedString绘制多色镂空字符串
- awk 实现数字螺旋打印
- .NET重构(一):抽象工厂模式实现登录
- configure: error: C++ preprocessor "/lib/cpp" fails sanity check
- Spring 中WebAppRootListener的误解
- Java向自定义文件夹中写入文件
- 定时器的计算问题
- Error:Error #2174
- 写Linux I2C设备驱动