美文网首页
iOS文字排版问题总结

iOS文字排版问题总结

作者: 是一个书生 | 来源:发表于2022-07-06 04:40 被阅读0次

1、UILabel行间距问题:设计师要求UILabel要有行间距,但是UILabel是没有这么一个直接暴露的属性的,想要修改lineSpacing,我们需要借助NSAttributedString来实现,需要注意计算文字上下留白,示意代码:

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.lineSpacing = 10 - (label.font.lineHeight - label.font.pointSize);
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
label.attributedText = [[NSAttributedString alloc] initWithString:label.text attributes:attributes];

2、UILabel内边距问题:自定义UILabel,然后重写drawTextInRect,示意代码:

import "CustomLabel.h"

@implementation CustomLabel

  • (instancetype)initWithFrame: (CGRect)frame {
    if (self = [super initWithFrame: frame]) {
    _textInsets = UIEdgeInsetsZero;
    }
    }
  • (void)drawTextInRect: (CGRect)rect {
    [super drawTextInRect: UIEdgeInsetsInsetRect(rect, _textInsets)];
    }
    @end

3、YYLabel、YYText

4、UITextView行间距问题,光标问题:

可以使用,
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.headIndent = 15; // <--- indention if you need it
paragraphStyle.firstLineHeadIndent = 15;
paragraphStyle.lineSpacing = 7; // <--- magic line spacing here!
NSDictionary *attrsDictionary =
@{ NSParagraphStyleAttributeName: paragraphStyle }; // <-- there are many more attrs, e.g NSFontAttributeName
self.textView.attributedText = [[NSAttributedString alloc] initWithString:@"Hello World over many lines!" attributes:attrsDictionary];
或者可以创建一个重新实现[UITextView styleString]的子类:
@implementation MBTextView

  • (id)styleString {
    return [[super styleString] stringByAppendingString:@"; line-height: 1.2em"];
    }
    @end

5、字间距问题:

NSString *labelText = label.text;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(space)}];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
label.attributedText = attributedString;
[label sizeToFit];

6、行高计算:

使用 string.boundingRect 方法。

相关文章

  • iOS文字排版问题总结

    1、UILabel行间距问题:设计师要求UILabel要有行间距,但是UILabel是没有这么一个直接暴露的属性的...

  • 资料篇

    参考资料 官方文档iOS文字排版(CoreText)那些事儿IOS CoreText.framework --- ...

  • iOS 富文本排版(一)

    富文本排版样式丰富详细,适合专业的文字展示. 下面就简单介绍下iOS 富文本的使用: 一 、 纯文字排版 (1)逐...

  • iOS富文本(NSAttributedString)

    参考iOS中关于AttributedString的那些事儿iOS 7中文字排版和渲染引擎——Text Kit 富文...

  • 丁香园iOS电话面试问题总结

    丁香园iOS电话面试问题总结 丁香园iOS电话面试问题总结

  • iOS按钮文字图片排版

    前几天看见一片文章关于使用自定义控件实现按钮文字图片排版的,个人感觉用起来还是不怎么方便, 先看下面常用的按钮布局...

  • iOS开发-UITextView文字排版

    UITextView文本排版 1.配置NSMutableParagraphStyle // MParaSty...

  • iOS小问题总结

    iOS小问题总结

  • Day12

    标准流:(流式布局) 从左到右,从上到下的依次布局 浮动的本质:解决文字和图片的排版问题。 总结:浮动的元素不会占...

  • CoreText入门知识

    很久以前写的文章搬到这里来放着。iOS开发中经常会遇到做一些文字排版的需求,文字图片混排的需求,在iOS7 以前一...

网友评论

      本文标题:iOS文字排版问题总结

      本文链接:https://www.haomeiwen.com/subject/yyvebrtx.html