label_设置行距、字距及计算含有行间距的label高度
//
// ViewController.m
// CNBlogs
//
// Created by PXJ on 16/5/27.
// Copyright © 2016年 PXJ. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString * conText = @"\n你的嘴角 微微上翘 性感的无可救药\n想像不到 如此心跳 你的一切都想要\n软性的饮料 上升的气泡 我将对你的喜好 一瓶装全喝掉\n这里最不缺就是热闹 你煽情给拥抱\n烛火在燃烧 有某种情调 眼神失焦了几秒\n关于你的舞蹈 你慵懒的扭动著腰 受不了\n你随风飘扬的笑 \n有迷迭香的味道 语带薄荷味的撒娇 对我发出恋爱的讯号\n你优雅的像一只猫 动作轻逸的围绕 爱的甜味蔓延发酵 暧昧来的刚好\n\n软性的饮料 上升的气泡 我将对你的喜好 一瓶装全喝掉\n这里最不缺就是热闹 你煽情给拥抱\n烛火在燃烧 有某种情调 眼神失焦了几秒\n关于你的舞蹈 你慵懒的扭动著腰 受不了\n你随风飘扬的笑 有迷迭香的味道 语带薄荷味的撒娇 对我发出恋爱的讯号\n你优雅的像一只猫 动作轻逸的围绕 爱的甜味蔓延发酵 暧昧来的刚好\n";
UIFont * font = [UIFont systemFontOfSize:12]; //字号
CGFloat lineSpace = 8;//行间距
CGFloat paragraphSpacing = 0;//段间距
CGSize labSize = CGSizeMake(400, 1000);//label宽高
NSNumber * textLengthSpace = @0.2;//字间距
NSDictionary * dic = [self setTextLineSpaceWithString:conText withFont:font withLineSpace:lineSpace withTextlengthSpace:textLengthSpace paragraphSpacing:paragraphSpacing];
CGSize size = [conText boundingRectWithSize:labSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
CGFloat sizeHeight = size.height;//lab的高度
// CGFloat sizeHeight = [self getSpaceLabelHeight:conText withFont:font withLineSpace:lineSpace size:labSize textlengthSpace:textLengthSpace paragraphSpacing:paragraphSpacing];
//
UILabel * lab = [[UILabel alloc] init];
lab.numberOfLines = 0;
lab.backgroundColor = [UIColor yellowColor];
lab.frame = CGRectMake(20,20, 400, sizeHeight);
lab.attributedText = [[NSAttributedString alloc] initWithString:conText attributes:dic];
[self.view addSubview:lab];
}
/*
*给UILabel设置行间距和字间距
*/
-(NSDictionary *)setTextLineSpaceWithString:(NSString*)str withFont:(UIFont*)font withLineSpace:(CGFloat)lineSpace withTextlengthSpace:(NSNumber *)textlengthSpace paragraphSpacing:(CGFloat)paragraphSpacing{
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
paraStyle.alignment = NSTextAlignmentLeft;
paraStyle.lineSpacing = lineSpace; //设置行间距
paraStyle.hyphenationFactor = 1.0;
paraStyle.firstLineHeadIndent = 0.0;
paraStyle.paragraphSpacingBefore = 0.0;
paraStyle.headIndent = 0;
paraStyle.tailIndent = 0;
NSDictionary *dic = @{NSFontAttributeName:font,
NSParagraphStyleAttributeName:paraStyle,
NSKernAttributeName:textlengthSpace
};
return dic;
}
/*
*计算UILabel的高度(带有行间距的情况)
*/
//-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withLineSpace:(CGFloat)lineSpace size:(CGSize)textSize textlengthSpace:(NSNumber *)textlengthSpace paragraphSpacing:(CGFloat)paragraphSpacing
//{
//
//
// NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
// paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
// paraStyle.alignment = NSTextAlignmentLeft;
// paraStyle.lineSpacing = lineSpace;
// paraStyle.paragraphSpacing = paragraphSpacing;
// paraStyle.hyphenationFactor = 1.0;
// paraStyle.firstLineHeadIndent = 0.0;
// paraStyle.paragraphSpacingBefore = 0.0;
// paraStyle.headIndent = 0;
// paraStyle.tailIndent = 0;
// NSDictionary *dic = @{NSFontAttributeName:font,
// NSParagraphStyleAttributeName:paraStyle,
// NSKernAttributeName:textlengthSpace
// };
// CGSize size = [str boundingRectWithSize:textSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
// return size.height;
//}
//
@end
效果图

label_设置行距、字距及计算含有行间距的label高度的更多相关文章
- CSS中字距,词距,首行缩进,字体大小,排版相关问题的探讨
先说明下,这是在谷歌浏览器下字体显示等问题做个研究,火狐下有点差异,不过火狐占有率低,而且显示的没有谷歌那么合理,不管它先.IE卡的要死,半死不活,也懒得深入研究这些细节,字体排版上不是强迫症,差别也 ...
- 可以简易设置文字内边距的EdgeInsetsLabel
可以简易设置文字内边距的EdgeInsetsLabel 最终效果: 源码: EdgeInsetsLabel.h 与 EdgeInsetsLabel.m // // EdgeInsetsLabel.h ...
- 一份新的lilypond谱子,能设置页边距和设置换页符了
给学生做的一份乐谱,这回能设置页边距了,以及设置换页符了. 顺带能设置一些代码片段(snippet),可以用热键代替使用 设置页边距的snippet: \paper { %双引号里面填页面大小 #(s ...
- word设置行距18磅
参考:word如何设置行距18磅 word设置行距18磅 选中需要设置的段落--"格式"菜单--段落--"缩进和间距"标签--在"行距"下拉 ...
- 动态计算Label高度
//1.设置该label的numberOfLines为0 self.titleLabel.numberOfLines = 0; //2.字体的设置要与之前相同 NSDictionary * at ...
- iOS7中计算UILabel中字符串的高度
iOS7中计算UILabel中字符串的高度 iOS7中出现了新的方法计算UILabel中根据给定的Font以及str计算UILabel的frameSize的方法.本人提供category如下: UIL ...
- js计算元素距离顶部的高度及元素是否在可视区判断
前言: 在业务当中,我们经常要计算元素的大小和元素在页面的位置信息.比如说,在一个滚动区域内,我要知道元素A是在可视区内,还是在隐藏内容区(滚动到外边看不到了).有时还要进一步知道,元素是全部都显示在 ...
- 精确计算微信小程序scrollview高度,全机型适配
众所周知,可以滑动的 scroll 组件在移动端非常的重要,几乎每个页面都要用到. 而小程序的 scroll-view 组件就比较坑了,非得指定一个高度才能正常使用.布局复杂的时候谁还给你算高度啊.. ...
- 根据文字动态计算Label高度或宽度
//根据已知的label宽度计算文字高度 CGRect rect = [reson boundingRectWithSize:CGSizeMake(label_W, 0) options:NSStri ...
随机推荐
- webStorm中的混乱代码格式化
Mac上 command + alt + l windows上 control + alt + l
- Quartz1.8.5例子(二)
/* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the ...
- nutch 采集效率问题
http://hi.baidu.com/jacklin/item/a8fbccf479f6a1d042c36a7c再附一篇:http://blog.csdn.net/laigood/article/d ...
- tyvj P1075 - 硬币游戏 博弈DP
P1075 - 硬币游戏 From price Normal (OI)总时限:10s 内存限制:128MB 代码长度限制:64KB 背景 Background 农民John的牛喜欢玩 ...
- HNOI2015滚粗记
HNOI2015滚粗记 经过两天的苦战,艰难的HNOI终于结束了.感觉这次HNOI自己还是收获了许多. \(Day1\)打的很是艰难,题目一下就有种晕头转向的感觉.开场\(20min\)自己还在读题时 ...
- zookeeper kazoo Basic Usage
http://kazoo.readthedocs.org/en/latest/basic_usage.html Basic Usage Connection Handling To begin usi ...
- LeetCode 面试:Add Binary
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...
- semijoin链接进行subquery unnesting.
drop table emp1; drop table dept1; create table emp1 as select * from emp; create table dept1 as sel ...
- Google Map API 学习六
今天其实收货很大的 1.new google.maps.Circle 就是如何在地图上标注一个圆 3.getAnimation 在这里是通过获取Marker是否存在动作,然后如果存在动作的话,就将动作 ...
- 折腾iPhone的生活——iPhone 5s 开启 assistive touch 后卡顿的问题
刚刚入手我的国行iPhone5s土狗灰,感觉倍棒~ 但是一上手就发现了一个问题:卡顿. 卡顿不仅体现在日常使用中,游戏中更加严重,当我玩水果忍者的时候,会发现切水果的画面都变得不流畅起来,这是拥有64 ...