UILabel 或 UITextView 在两行中带有第一个字母非常大的字体
Posted
技术标签:
【中文标题】UILabel 或 UITextView 在两行中带有第一个字母非常大的字体【英文标题】:UILabel or UITextView with first letter very big font in two line 【发布时间】:2017-06-20 07:36:28 【问题描述】:如何自定义 UILabel 或 UITextView 像给定的图像,即大“T”显示在标签的两行中。或者请为此建议任何图书馆:
【问题讨论】:
【参考方案1】:我想你会在here找到很多关于这个话题的有用信息
所以你需要使用 CoreText 来实现你的目标。
【讨论】:
不幸的是,此链接中的 github 示例不完整。但我想我有一个实现它的方向,为此我需要学习 CoreText 框架的一些核心概念。如果你已经有 CoreText 框架的经验,你能给我发一个工作示例吗?谢谢 很遗憾,我对 CoreText 的了解不多。但我认为您可以查看其他人如何使用它,这里有一堆开源项目:cocoacontrols.com/search?q=coretext【参考方案2】:试试这个
请注意这仅适用于 TextView
NSString *myTextString =@"Your are qualified as a lawyer specializing in marine cases, shipping, arbitration and commercial litigation";
UIBezierPath * imgRect = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 40, 40)]; // you can change this as per your needs
txtViewDescription.textContainer.exclusionPaths = @[imgRect];
txtViewDescription.text = myTextString;
txtViewDescription.text = [txtViewDescription.text substringFromIndex:1]; // Remove first letter from string
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 80, 80)];// you can change this as per your needs
lbl.font = [UIFont fontWithName:Lato_BOLD size:50];
lbl.text = [myTextString substringToIndex: 1]; // get first letter of string
[self.view bringSubviewToFront:lbl];
[self.view addSubview:lbl];// if your textview added to self.view else change with your view
【讨论】:
【参考方案3】:这是我在操场上实现的(Swift 3 answer):
import UIKit
// declare the label:
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
label.numberOfLines = 2
label.backgroundColor = UIColor.white
// this should be the desired text
let myString = "This is my pretty string that should contains a couple of lines."
// setup the attributed string
let content = NSMutableAttributedString(string: myString, attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 20)])
content.setAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 40), NSForegroundColorAttributeName: UIColor.red], range: NSRange(location: 0, length: 1))
// assign the string to the label
label.attributedText = content
输出:
【讨论】:
【参考方案4】:@KKRocks 在Swift 3.0
中的answer
let yourString = "This easy granola is inspired by a grain-free granola from Costco that I love. The ingredient list is short so I figured that I could make it at home quite easily."
let lbl = UILabel(frame: CGRect(x: 5, y: 5, width: 30, height: 30))
lbl.font = UIFont.boldSystemFont(ofSize: 40)
lbl.text = yourString[0]
txtView.addSubview(lbl)
txtView.bringSubview(toFront: lbl)
let bezierPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 35, height: 30))
txtView.textContainer.exclusionPaths = [bezierPath]
txtView.text = yourString.substring(from: 1)
// txtView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10)
【讨论】:
以上是关于UILabel 或 UITextView 在两行中带有第一个字母非常大的字体的主要内容,如果未能解决你的问题,请参考以下文章
使用不可见的 UILabel 并触发 UITextView 事件或不使用 UILabel 并处理 UITextView 点击识别器
如何在滚动视图中使 UIImage 低于 UILabel 或 UITextView