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

时间:2017-06-20 07:36:28

标签: ios swift uikit uilabel uitextview

如何自定义UILabel或UITextView,就像给定的图像一样,i-e big“T”显示在标签的两行中。或者请为此建议任何图书馆:

enter image description here

4 个答案:

答案 0 :(得分:0)

我认为你会在here中找到关于这个主题的很多有用的信息

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

答案 1 :(得分:0)

试试这个

请注意,仅适用于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

答案 2 :(得分:0)

这是我在操场上实施的( Swift 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

答案 3 :(得分:0)

@ {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)