drawInRect 绘制文字

本文介绍了在iOS开发中如何使用drawInRect方法来绘制文字,包括设置文字内容、位置、字体、行宽模式和对齐方式。特别强调了在iOS7之前的用法,并提到了NSAttributedString的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iOS7 之前,如下:

Objective-C

NSString *str = @"Hello World"

[str drawInRect: CGRectMake(10,10,300, 30)

withFont:[UIFont fontWithName: @"Courier" size: kCellFontSize]

lineBreakMode: NSLineBreakByTruncatingTail

alignment: NSTextAlignmentRight

]

NSParagraphStyleAttributeName

此属性的值是 NSParagraphStyle 对象。此属性用于将多个属性应用到的文本的范围。如果不指定此特性的字符串使用的默认段落属性,返回的 defaultParagraphStyle 方法的 NSParagraphStyle 。


UIFont *font = [UIFont fontWithName:@"Courier" size:kCellFontSize];

/// Make a copy of the default paragraph style
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
/// Set line break mode
paragraphStyle
.lineBreakMode = NSLineBreakByTruncatingTail;
/// Set text alignment
paragraphStyle
.alignment = NSTextAlignmentRight;

NSDictionary *attributes = @{ NSFontAttributeName: font,
                   
NSParagraphStyleAttributeName: paragraphStyle };

[text drawInRect:rect withAttributes:attributes];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值