【IOS】将字体大小不同的文字底部对齐
从WP转IOS了,还是放不下......
在项目中,要实现如图多个不同大小的文字 底部对齐的效果 像下面这样:
(想要的效果)
以为用三个UIFont不同的UILabel 之后让他们底部对齐就可以了,但是效果是下面这样的:
(不想要的效果)
底部完全不对齐呀,为什么"1314"比两边高出了那么多呀!!!!强迫症不能忍呀!!!
--------------------------------------------------------------------------------
对比:
1.在Windows Phone中 控件是相对布局
Windows Phone里面TextBlock需要设置一个固定的 宽度 和 高度 (至少我是这样做的)
TextBlock不但可以将文字设置为水平居中、偏左、偏右,还能通过属性
text.VerticalContentAlignment=Top/Center/Bottom将文字置于顶部/居中/底部。(WP中发现也只能水平方向的)
2.在IOS中 控件是绝对布局: 对于一个控件 需要设置它的frame(包括起始位置、高和宽)
可以通过以下方法直接获取一个字符串的自适应的高度和宽度:(这点觉得比WindowsPhone好)
CGSize labelSize = [@"1314" sizeWithAttributes:@{NSFontAttributeName:kLabelFont}];
而原生的UIlabel只能设置textAlignment文字的水平偏移属性,而无法直接更改竖直方向的偏移。
WindowsPhone中要实现上面的效果,只需将三个TextBlock置底,TextBlock内容居下,即可对齐。
(这一点我突然的不确定了,好像很多时候我都是直接设置margin来手动对齐的,有空回WindowsPhone看看再修改。)
[2016.5.25修改] WP中TextBlock也没有VerticalContentAlignment属性...
弯路:
所以在IOS处理这个问题的时候,我想着既然三个控件的底部都是对齐了的,只是高度不一样,能否仿照着WindowsPhone那样,
将较高UILabel的内容("1314")竖直偏移到最底下呢?
经过查阅 UILabel找到了这些:
不能直接调用 只能通过继承来重写
// override points. can adjust rect before calling super. (在调用父类前 可以调整文本内容的位置TextRect)
// label has default content mode of UIViewContentModeRedraw
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines; //重写来重绘文字区域
- (void)drawTextInRect:(CGRect)rect; //重写来重绘文本 重写时调用super可以按默认图形属性绘制
所以为了改变text的位置, 我们只需要改变textRectForBounds的返回值rect,并且根据这个rect重绘文本就可以了
1、关于contentMode,该模式为视图提供了多种模式用以适用框架矩形。如果对除UIVewContentModeRedraw之外的模式(如UIViewContentModeScaleToFill)都不满足需求,或者说有特定需要自定义绘制视图,可以设置为此值。那么将在视图适应框架矩形变化时缺省自动调用setNeedsDispllay或setNeedsDisplayInRect:,从而重绘视图。
2、而向视图发送setNeedsDisplay(或setNeedsDisplayInRect:)消息时,无论此时contentMode为何种模式,都将强制调用drawRect:
主要代码:(这个代码网上一大把,自己找去)
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{ //方法里的形参bounds指的是UILabel的bounds
CGRect rc = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines]; //text的bounds
switch (_verticalAlignment) {
case VerticalAlignmentTop:
rc.origin.y = bounds.origin.y;
break;
case VerticalAlignmentBottom:
rc.origin.y = bounds.origin.y + bounds.size.height - rc.size.height;
break;
case VerticalAlignmentMiddle:
default:
rc.origin.y = bounds.origin.y + (bounds.size.height - rc.size.height) / ;
break;
}
return rc;
}
- (void)drawTextInRect:(CGRect)rect
{
CGRect rc = [self textRectForBounds:rect limitedToNumberOfLines:self.numberOfLines];
[super drawTextInRect:rc];
}
然而发现:对于自适应宽高(CGSize)的Label, 设置了verticalAlignment属性基本上没多大区别
origin.x origin.y 均为零 并且SIZE区别几乎没有 所有也不会有多大区别。
这里我进行了一个对比:对于字符串@"1314",设置不同的UIFont 观察UILabel和内部的textRect的大小差别
UIFont 1 50 100
rc(text) 1.000000,2.333333 105.666667,61.000000 208.666667,120.666667
bounds(label) 1.000000,2.193359 105.541992,60.667969 208.349609,120.335938
相差 0,0.139974 0.124675,0.332031 0.317058, 0.330729
rc居然还要比bounds大那么一点点,我认为为了文字不被截取,文字大小还是比其文字边框还要小的,
并且针对不同大小的UIFont或字体类型,这种小的程度还是不一样的。
所以就算设置verticalAlignment属性也发现不了什么区别。
如果只将文字大小设置更大,导致rc比bounds大,那么文字部分就可能会被截取;
如果只增加UILabel的大小,导致rc比bounds小,那么就会看到除了文字外多余的空白区域;
对于后两者,设置verticalAlignment属性,就能够明显的看到上下偏移了。
所以这种方案对于实现所需效果无效。
终点:
找呀找呀 又找到了另外一种方法,可以通过attributedText属性让UILabel显示富文本
实现一个字符串中包含不同颜色、字号等。
我封装了一个方法:
// 获取带有不同样式的文字内容
//stringArray 字符串数组
//attributeAttay 样式数组
- (NSAttributedString *)attributedText:(NSArray*)stringArray attributeAttay:(NSArray *)attributeAttay{
// 定义要显示的文字内容
NSString * string = [stringArray componentsJoinedByString:@""]; //拼接传入的字符串数组
// 通过要显示的文字内容来创建一个带属性样式的字符串对象
NSMutableAttributedString * result = [[NSMutableAttributedString alloc] initWithString:string];
for(NSInteger i = ; i < stringArray.count; i++){
// 将某一范围内的字符串设置样式
[result setAttributes:attributeAttay[i] range:[string rangeOfString:stringArray[i]]];
}
// 返回已经设置好了的带有样式的文字
return [[NSAttributedString alloc] initWithAttributedString:result];
}
以及其使用
NSDictionary *attributesExtra = @{NSFontAttributeName:kLabelFont,//字号12
NSForegroundColorAttributeName: [UIColor orangeColor]};
NSDictionary *attributesPrice = @{NSFontAttributeName:kPriceFont,//字号18
NSForegroundColorAttributeName: [UIColor orangeColor]};
NSAttributedString *attributedString = [self attributedText:@[@"¥", @"1314", @"起"]
attributeAttay:@[attributesExtra,attributesPrice,attributesExtra]];
UILabel *price = [[UILabel alloc]init];
price.attributedText = attributedString;
CGRect rect = [attributedString boundingRectWithSize:CGSizeMake(self.width / , ) options:NSStringDrawingUsesLineFragme ntOrigin context:nil]; //此方法获取到的是自适应的Rect,而不是CGSize 最大Size值为CGSizeMake(self.width / 2, 100)
price.frame = CGRectMake(0,0,rect.size.width, rect.size.height);
【IOS】将字体大小不同的文字底部对齐的更多相关文章
- IOS开发根据字体大小等获取文字所占的高度
Model *model = self.modelArr[indexPath.row]; //根据label文字获取CGRect NSMutableParagraphStyle *paragraphS ...
- ios根据字体大小设置
, , , , , , , , , , , }; //这么多字体,从大到小挨个尝试 ; UIFont *font; ; i < array_length; i++) { font = [font ...
- iOS字体换算 PS的字体大小 <=>iOS上字体大小
- [转帖]自动调整TextView字体大小以适应文字长度
package com.test.android.textview; import android.content.Context; import android.graphics.Paint; im ...
- Android-修改TabWidget字体大小颜色及对齐
在Android中,我们可以定义TabWidget来分页.在上一篇文章中有说到使用TabWidget定义Tab分页布局,但大部分用户可能会觉得默认的字体有点小,但Tab选项卡默认又不能设定字体大小,如 ...
- [转]div内容底部对齐
本文转自:http://blog.csdn.net/hellomy/article/details/5889833 <html> <head> <meta http-eq ...
- (转)解决NSMutableAttributedString富文本,不同文字大小水平轴对齐问题(默认底部对齐)
默认是底部对齐,其实对的也不齐, 目标效果: 代码: NSBaselineOffsetAttributeName 基线偏移量: 调整: NSBaselineOffsetAttributeName的值 ...
- UISegmentedControl字体大小,颜色,选中颜色,左边椭圆,右边直线的Button 解决之iOS开发之分段控制器UISegmentedControl
NSArray *segmentedArray = [NSArrayarrayWithObjects:STR(@"Mynews"),STR(@"Systemmes ...
- 根据html容器大小和显示文字多少调节字体大小
在做html相关的东西的时候经常会遇到这样的问题,容器大小(长x宽)固定,容器包含内容(特指文字)多少不固定,这个时候就让人很苦恼了,将字体大小设置成多少才合适呢?下面看看我的解决思路: 首先要知道网 ...
随机推荐
- java nio系列文章
java nio系列教程 基于NIO的Client/Server程序实践 (推荐) java nio与并发编程相关电子书籍 (访问密码 48dd) 理解NIO nio学习记录 图解ByteBuff ...
- ASP.NET MVC - 定制属于你自己的ViewEngine
http://blog.csdn.net/jackvs/article/details/7788743 ASP.NET MVC出来这么久了,心中却又很多的疑惑:为什么所有的View都要放在Views目 ...
- [LeetCode] Encode and Decode Strings 加码解码字符串
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 研究Extension和Category的一个例子
Category: 1. 无法添加实例变量 2.将类的实现分散到多个不同文件或多个不同框架中. Extension: 1. 可以添加实例变量 注: 如果Category的头文件中也使用Extensio ...
- jQuery 事件方法
事件方法触发器或添加一个函数到被选元素的事件处理程序. 下面的表格列出了所有用于处理事件的 jQuery 方法. 方法 描述 bind() 向匹配元素附加一个或更多事件处理器 blur() 触发.或将 ...
- podfile The dependency `` is not used in any concrete target
内容提要: podfile升级之后到最新版本,pod里的内容必须明确指出所用第三方库的target,否则会出现The dependency `` is not used in any concrete ...
- 【BZOJ-3143】游走 高斯消元 + 概率期望
3143: [Hnoi2013]游走 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2264 Solved: 987[Submit][Status] ...
- bash的管道符与重定向
管道符"|"可以用来将前面的程序的标准输出stdout(=1)重定向到后一个程序的stdin(=0),但是忽略了stderr. 在bash中使用2>&1 可以表示将s ...
- SQL注入判断方法总结(持续更新)
http://e.com/1.php?id=1 http://e.com/1.php?id=1-- sd http://e.com/1.php?id=aaa http://e.com/1.php?id ...