UILabel或UITextView与第一个字母非常大的字体在两行

问题描述:

如何自定义UILabel或UITextView像给定的图像我大e“T”显示在标签的两行中。或请建议任何库为:UILabel或UITextView与第一个字母非常大的字体在两行

enter image description here

我认为你会在here找到很多有用的信息的有关此主题的

所以你需要使用CoreText来实现自己的目标。

+0

不幸的是在这个环节GitHub的例子是不完整的。但我想我有一个方向来实现它,因为我需要学习CoreText框架的一些核心概念。如果你已经有CoreText框架的经验,你能给我一个工作的例子吗?谢谢 – iMemon

+0

不幸的是我对CoreText没有太多的了解。但我认为你可以检查其他人如何使用它,在这里你有一堆开源项目:https://www.cocoacontrols.com/search?q=coretext – Robert

试试这个

注意仅此为TextView的

 NSString *myTextString [email protected]"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回答):

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 

输出:

enter image description here

@ KKRocks的answerSwift 3.0

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)