Skip to content

Instantly share code, notes, and snippets.

@CH3COOH
Created March 10, 2017 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CH3COOH/7a291846f5a8a96ad3a15609277f3060 to your computer and use it in GitHub Desktop.
Save CH3COOH/7a291846f5a8a96ad3a15609277f3060 to your computer and use it in GitHub Desktop.
NSTextAttachmentが含まれているNSAttributedStringでは行間をあけることができない http://blog.ch3cooh.jp/entry/20170310/1489123042
//
// ViewController.swift
// MultipleLines
//
// Created by WADAKENJI on 2017/03/10.
// Copyright © 2017年 ch3cooh.jp. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
func setMultipleText() {
let str = NSMutableAttributedString()
//プロ生ちゃんアイコンを追加する
let number = NSTextAttachment()
if let image = UIImage(named: "sd06") {
number.image = image
number.bounds = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
}
str.append(NSAttributedString(attachment: number))
//長いテキストを追加する
let longText = "プロ生ちゃん「ああああああああああああああああああああああああああああああああああああああああああああああああああああああ」"
str.append(NSAttributedString(string: longText))
//属性を設定する
let style = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
let font = UIFont.boldSystemFont(ofSize: 19)
// style.minimumLineHeight = font.lineHeight + 30
// style.maximumLineHeight = font.lineHeight + 30
style.lineSpacing = 30
let attr: [String : Any] = [
NSFontAttributeName: font,
NSParagraphStyleAttributeName: style
]
//属性を付与する
str.addAttributes(attr, range: NSRange(location: 0, length: str.length))
//ラベルにNSAttributedStringを設定する
label.attributedText = str
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
label.lineBreakMode = .byCharWrapping
label.numberOfLines = 0
label.backgroundColor = UIColor.cyan
setMultipleText()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment