最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】swift中显示指定字数的文字

Swift crifan 3028浏览 0评论

现在是,显示完整的文字:

show full text of ios

希望变成这样:

show some number of chars

即,给定字体大小后,可以显示指定字数的文字,多余的文字,自动变成…

 

【解决过程】

1.搜:

swift UILabel show designated number text

其实自己指定宽度,好像也就达到了效果了:

       currentTeamName = "苏州达云网络科技有限公司"
       
        currentTeamLabel = UILabel(frame: CGRectMake(
            10,
            0,
            150,//self.view.bounds.width - 108,
            NAVI_BAR_HEIGHT))
        currentTeamLabel.textColor = UIColor.whiteColor()
        currentTeamLabel.textAlignment = NSTextAlignment.Left
        currentTeamLabel.center.y = statusView.frame.height + tabbgView.frame.height / 2
        currentTeamLabel.font = UIFont.boldSystemFontOfSize(FONT_CURRENT_TEAM_NAME)
        currentTeamLabel.text = currentTeamName
        self.view.addSubview(currentTeamLabel)

 

但是此处想要做的更智能的:

希望计算出单个字符的宽度

然后再去算出限制的个数

显示宽度就是用:限制的显示个数的宽度+多余的半个或1个点字符宽度

2.问题变成:

如何知道单个汉字字符点显示的宽度

搜:

swift UILabel single char width

参考:

iphone – How to calculate UILabel width based on text length? – Stack Overflow

iphone – How to calculate the width of a text string of a specific font and font-size? – Stack Overflow

iphone – How to set font size to fill UILabel height? – Stack Overflow

对应代码为:

import UIKit

func calcLabelTextSize(text:String, font:UIFont) -> CGSize {
    let textLabel:UILabel = UILabel()
    textLabel.text = text
    textLabel.font = font
    textLabel.sizeToFit()
    let labelTextSize:CGSize = textLabel.bounds.size

    return labelTextSize
}

        currentTeamName = "苏州达云网络科技有限公司"
        //currentTeamName = "苏州达云网络科技有"
        //currentTeamName = "苏州达云网络科技"
       
        //let currentTeamNameFont:UIFont = UIFont.systemFontOfSize(FONT_CURRENT_TEAM_NAME)
        let currentTeamNameFont:UIFont = UIFont.boldSystemFontOfSize(FONT_CURRENT_TEAM_NAME)
       
        let singleZhcnChar:String = "单" //single Zh-cn character for calc width
        let singleZhcnCharWidth:CGFloat = calcLabelTextSize(singleZhcnChar,
            font: currentTeamNameFont).width
        print("singleZhcnCharWidth=\(singleZhcnCharWidth)")
        let zhcnCharNumLimit:Int = 8
        let currentTeamNamelimitWidth:CGFloat = singleZhcnCharWidth * CGFloat(zhcnCharNumLimit)
            //add some more extra width to let exceed char show as "..."
            + singleZhcnCharWidth*0.9
       
        print("currentTeamNamelimitWidth=\(currentTeamNamelimitWidth)")
       
        currentTeamLabel = UILabel(frame: CGRectMake(
            0,
            0,
            currentTeamNamelimitWidth,//150,//self.view.bounds.width - 108,
            HEIGHT_NAVI_BAR))
        print("currentTeamLabel.frame=\(currentTeamLabel.frame)")
        currentTeamLabel.textColor = UIColor.whiteColor()
        currentTeamLabel.textAlignment = NSTextAlignment.Left
        currentTeamLabel.center.y = naviView.frame.height / 2
        currentTeamLabel.font = currentTeamNameFont
        currentTeamLabel.text = currentTeamName

效果为:

ios swift can show designated text

转载请注明:在路上 » 【已解决】swift中显示指定字数的文字

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.165 seconds, using 22.10MB memory