jjzjj

swift - 表节标题 : multi-line/word wrapping

coder 2023-09-04 原文

我正在尝试制作一个表,其中的节标题可以是长字符串。我以为我的设置正确(动态行数,自动换行设置),但字符串只是在末尾被截断了。请注意,节标题的大小为 80,在其他地方,这足以显示大约 3 行文本。

// Format section header
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    header.contentView.backgroundColor = mainColorBlue
    header.textLabel.textColor = UIColor.whiteColor()

    header.textLabel.textAlignment = NSTextAlignment.Left
    header.textLabel.numberOfLines = 0 // Dynamic number of lines
    header.textLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
    header.textLabel.font = UIFont(name: "HelveticaNeue-Thin", size: 16)!
    header.textLabel.text = objectsArray[section].sectionName

}

最佳答案

您将必须创建自定义 headerView。这就是我所做的 - Swift 3,您必须自定义它以供您使用:

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
//Need to create a label with the text we want in order to figure out height
    let label: UILabel = createHeaderLabel(section)
    let size = label.sizeThatFits(CGSize(width: view.width, height: CGFloat.greatestFiniteMagnitude))
    let padding: CGFloat = 20.0
    return size.height + padding
}

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UITableViewHeaderFooterView()
    let label = createHeaderLabel(section)
    label.autoresizingMask = [.flexibleHeight]
    headerView.addSubview(label)
    return headerView
}

func createHeaderLabel(_ section: Int)->UILabel {
    let widthPadding: CGFloat = 20.0
    let label: UILabel = UILabel(frame: CGRect(x: widthPadding, y: 0, width: self.view.width - widthPadding, height: 0))
    label.text = sectionTextArray[section]// Your text here
    label.numberOfLines = 0;
    label.textAlignment = NSTextAlignment.left
    label.lineBreakMode = NSLineBreakMode.byWordWrapping
    label.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.subheadline) //use your own font here - this font is for accessibility 
    return label
}    

关于swift - 表节标题 : multi-line/word wrapping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31171404/

有关swift - 表节标题 : multi-line/word wrapping的更多相关文章

  1. ruby-on-rails - 使用 Rmagick 或 ImageMagick 在背景上放置标题 - 2

    我有一张背景图片,我想在其中添加一个文本框。我想弄清楚如何将标题放置在其顶部的正确位置。(我使用标题是因为我需要自动换行功能)。现在,我只能让文本显示在左上角,但我需要能够手动定位它的开始位置。require'RMagick'require'Pry'includeMagicktext="Loremipsumdolorsitamet"img=ImageList.new('template001.jpg')img 最佳答案 这是使用convert的ImageMagick命令行的答案。如果你想在Rmagick中使用这个方法,你必须自己移植

  2. ruby parslet : parsing multiple lines - 2

    我正在寻找一种匹配多行Parslet的方法。代码如下所示:rule(:line){(match('$').absent?>>any).repeat>>match('$')}rule(:lines){line.repeat}但是,lines将始终以无限循环结束,这是因为match('$')将无休止地重复以匹配字符串的结尾。是否可以匹配可以为空的多行?irb(main)>lines.parse($stdin.read)Thisisamultilinestring^D应该匹配成功。我错过了什么吗?我还尝试了(match('$').absent?>>any.maybe).repeat(1)>>

  3. ruby - 如何跳过 CSV 文件的第一行并将第二行作为标题 - 2

    有没有办法跳过CSV文件的第一行,让第二行作为标题?我有一个CSV文件,第一行是日期,第二行是标题,所以我需要能够在遍历它时跳过第一行。我尝试使用slice但它会将CSV转换为数组,我真的很想将其读取为CSV,以便我可以利用header。 最佳答案 根据您的数据,您可以使用另一种方法和skip_lines-option此示例跳过所有以#开头的行require'csv'CSV.parse(DATA.read,:col_sep=>';',:headers=>true,:skip_lines=>/^#/#Markcomments!)do|

  4. ruby - 使用 ruby​​ gem net-ssh-multi 同时在多个服务器上执行 sudo 命令 - 2

    在previousquestion中我想出了如何在多个服务器上启动经过密码验证的sshsession来运行单个命令。现在我需要能够执行“sudo”命令。问题是,net-ssh-multi没有分配sudo需要运行的伪终端(pty),导致以下错误:[127.0.0.1:stderr]sudo:sorry,youmusthaveattytorunsudo根据documentation,可以通过调用channel对象的方法来分配伪终端,但是,以下代码不起作用:它会生成上面的“notty”错误:require'net/ssh'require'net/ssh/multi'Net::SSH::Mul

  5. ruby - 如何将字符串格式的毫秒数转换为 HH :MM:SS format in Ruby in under 3 lines of code? - 2

    @scores_raw.eachdo|score_raw|#belowiscodeiftimewasbeingsentinmillisecondshh=((score_raw.score.to_i)/100)/3600mm=(hh-hh.to_i)*60ss=(mm-mm.to_i)*60crumbs=[hh,mm,ss]sum=crumbs.first.to_i*3600+crumbs[1].to_i*60+crumbs.last.to_i@scoressum,:hms=>hh.round.to_s+":"+mm.round.to_s+":"+ss.round.to_s}@score

  6. ruby - Rubocop 规则 : Never use 'do' with multi-line 'while - 2

    我有以下代码#coloursarandomcellwithacorrectcolourdefcolour_random!whiletruedocol,row=rand(columns),rand(rows)cell=self[row,col]ifcell.empty?thencell.should_be_filled??cell.colour!(1):cell.colour!(0)breakendendend做什么并不重要,尽管它应该很明显。关键是Rubocop给了我一个警告Neveruse'do'withmulti-line'while为什么我不应该那样做?那我该怎么办呢?

  7. ruby - 如何在 selenium-webdriver 中获取窗口标题、ID 和名称? - 2

    我正在尝试从selenium-webdriver(ruby)实现以下方法get_all_window_idsget_all_window_titlesget_all_window_names我运行了SeleniumIDE并将我的脚本导出到RubyTest::Unit。另存为.rb使用AptanaStudio3打开我的脚本进行编辑初始代码片段如下:require"rubygems"require"selenium-webdriver"require"test/unit"classSwitchToPopup3我不断得到的错误是NoMethodError:undefinedmethod`ge

  8. ruby - 在 ruby​​ 中,file.readlines.each 并不比 file.open.each_line 快,为什么? - 2

    只是为了分析我的iis日志(奖励:碰巧知道iislog是用ASCII编码的,errrr..)这是我的ruby代码1.readlinesDir.glob("*.log").eachdo|filename|File.readlines(filename,:encoding=>"ASCII").eachdo|line|#commentlineifline[0]=='#'nextelseline_content=line.downcase#justcareaboutfirstonematched_keyword=keywords.select{|e|line_content.include?e

  9. 微信小程序顶部标题栏与胶囊对齐 - 2

    介绍    最近在做微信小程序时,顶部标题栏总是与胶囊对不齐。往往是在这款手机上对齐了,在另外一款手机差很多。我在查阅资料后,提出了一种方法解决这个问题,即:在页面onLoad或组件created时,利用微信小程序提供的API,获取系统状态栏高度和胶囊信息,进而动态调整顶部标题栏样式。在苹果、小米、荣耀手机做验证,能做到精准对齐。理论        胶囊样式应该是垂直居中,有1px的border,border-radius为18px。        若要使顶部标题栏与胶囊对齐,则其高度必须是导航栏高度,标题栏内容也要垂直居中,顶部标题栏的外边距或内边距必须是状态栏高度。        如果顶部

  10. ruby each_line 也读取换行符? - 2

    我正在尝试从文本文件中读取数据并将其与帖子字符串连接起来。当文件中只有一行时,它工作正常。但是有2行,我的请求失败了。each_line是否读取换行符?我该如何纠正它?File.open('sfzh.txt','r'){|f|f.each_line{|row|send(row)}我确实通过拆分和额外的定界符绕过了这个问题。但它看起来很丑。 最佳答案 是的,each_line包括换行符。但是您可以使用chomp轻松剥离它们:File.foreach('test1.rb')do|line|sendline.chompend

随机推荐