├── ss.png ├── Tanzaku.xcodeproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── project.pbxproj ├── README.md ├── Tanzaku ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Info.plist ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── AppDelegate.swift ├── ViewController.swift └── Tanzaku.swift ├── LICENSE └── .gitignore /ss.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/usagimaru/Tanzaku/HEAD/ss.png -------------------------------------------------------------------------------- /Tanzaku.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 短冊 Tanzaku 2 | 3 | UIKit に適合した縦書き実装。 4 | 5 | 6 | 7 | 8 | # 実装済み 9 | 10 | - 縦書き 11 | - 複数行 12 | - 文字単位折り返し 13 | - 省略処理 14 | - 上略 15 | - 中略 16 | - 下略 17 | - 行間 18 | - フォント 19 | - 文字色 20 | - sizeToFit() 対応 21 | - Auto Layout 対応 22 | 23 | # 制約・非対応項目 24 | 25 | - 適切な禁則処理(実装予定) 26 | - NSAttributedString による装飾 27 | - 段落 28 | - ハイパーリンク挿入 29 | 30 | # 実装予定一覧 31 | 32 | - 禁則処理 33 | - 縦中横 34 | - 約物半角化 35 | - 影描画 36 | - ルビ 37 | -------------------------------------------------------------------------------- /Tanzaku/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Satori Maru. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Tanzaku/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | .DS_Store 6 | 7 | ## Build generated 8 | build/ 9 | DerivedData/ 10 | 11 | ## Various settings 12 | *.pbxuser 13 | !default.pbxuser 14 | *.mode1v3 15 | !default.mode1v3 16 | *.mode2v3 17 | !default.mode2v3 18 | *.perspectivev3 19 | !default.perspectivev3 20 | xcuserdata/ 21 | 22 | ## Other 23 | *.moved-aside 24 | *.xcuserstate 25 | 26 | ## Obj-C/Swift specific 27 | *.hmap 28 | *.ipa 29 | *.dSYM.zip 30 | *.dSYM 31 | 32 | ## Playgrounds 33 | timeline.xctimeline 34 | playground.xcworkspace 35 | 36 | # Swift Package Manager 37 | # 38 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 39 | # Packages/ 40 | .build/ 41 | 42 | # CocoaPods 43 | # 44 | # We recommend against adding the Pods directory to your .gitignore. However 45 | # you should judge for yourself, the pros and cons are mentioned at: 46 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 47 | # 48 | # Pods/ 49 | 50 | # Carthage 51 | # 52 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 53 | # Carthage/Checkouts 54 | 55 | Carthage/Build 56 | 57 | # fastlane 58 | # 59 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 60 | # screenshots whenever they are needed. 61 | # For more information about the recommended setup visit: 62 | # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md 63 | 64 | fastlane/report.xml 65 | fastlane/Preview.html 66 | fastlane/screenshots 67 | fastlane/test_output 68 | -------------------------------------------------------------------------------- /Tanzaku/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Tanzaku/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // Tanzaku 4 | // 5 | // Created by usagimaru on 2017.03.12. 6 | // Copyright © 2017年 usagimaru. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Tanzaku/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // 4 | // Created by usagimaru on 2016/12/14. 5 | // Copyright © 2016年 usagimaru. All rights reserved. 6 | // 7 | 8 | import UIKit 9 | 10 | class ViewController: UIViewController { 11 | @IBOutlet weak var tanzaku: Tanzaku! 12 | @IBOutlet weak var slider: UISlider! 13 | @IBOutlet weak var truncationControl: UISegmentedControl! 14 | @IBOutlet weak var numberOfLines: UIStepper! 15 | @IBOutlet weak var lineCountLabel: UILabel! 16 | @IBOutlet weak var sampleLabel: UILabel! { 17 | didSet { 18 | sampleLabel.layer.borderColor = UIColor.red.cgColor 19 | sampleLabel.layer.borderWidth = 1.0 20 | } 21 | } 22 | 23 | override func viewDidLoad() { 24 | super.viewDidLoad() 25 | 26 | let size = CGFloat(20) 27 | slider.value = Float(size) 28 | 29 | let lineCount = 0 30 | 31 | let text = "祇園精舎の鐘の声、諸行無常の響きあり。沙羅双樹の花の色、盛者必衰のことわりをあらはす。奢れる人も久しからず、唯春の夜の夢のごとし。たけき者も遂には滅びぬ、偏に風の前の塵に同じ。" 32 | tanzaku.text = text 33 | tanzaku.font = UIFont(name: "Hiragino Sans", size: size)! 34 | tanzaku.textColor = UIColor.blue 35 | tanzaku.truncationMode = .byCharWrapping 36 | tanzaku.numberOfLines = UInt(lineCount) 37 | tanzaku.textAlignment = .top 38 | 39 | sampleLabel.text = text 40 | sampleLabel.lineBreakMode = tanzaku.truncationMode.convertToNativeLineBreakMode() 41 | sampleLabel.numberOfLines = lineCount 42 | 43 | lineCountLabel.text = "\(lineCount)" 44 | numberOfLines.value = Double(lineCount) 45 | } 46 | 47 | override func didReceiveMemoryWarning() { 48 | super.didReceiveMemoryWarning() 49 | // Dispose of any resources that can be recreated. 50 | } 51 | 52 | @IBAction func sliderAction(_ sender: Any) { 53 | if let font = UIFont(name: tanzaku.font.fontName, size: CGFloat(slider.value)) { 54 | tanzaku.font = font 55 | } 56 | } 57 | 58 | @IBAction func truncationAction(_ sender: Any) { 59 | switch truncationControl.selectedSegmentIndex { 60 | case 0: 61 | tanzaku.truncationMode = .byCharWrapping 62 | break 63 | case 1: 64 | tanzaku.truncationMode = .byTruncatingHead 65 | break 66 | case 2: 67 | tanzaku.truncationMode = .byTruncatingMiddle 68 | break 69 | case 3: 70 | tanzaku.truncationMode = .byTruncatingTail 71 | break 72 | case 4: 73 | tanzaku.truncationMode = .byJustification 74 | break 75 | default: 76 | break 77 | } 78 | 79 | sampleLabel.lineBreakMode = tanzaku.truncationMode.convertToNativeLineBreakMode() 80 | } 81 | 82 | @IBAction func linesAction(_ sender: Any) { 83 | lineCountLabel.text = "\(Int(numberOfLines.value))" 84 | tanzaku.numberOfLines = UInt(numberOfLines.value) 85 | sampleLabel.numberOfLines = Int(numberOfLines.value) 86 | } 87 | 88 | } 89 | 90 | -------------------------------------------------------------------------------- /Tanzaku.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 5136A08F1E7599D900D4ED48 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5136A08E1E7599D900D4ED48 /* AppDelegate.swift */; }; 11 | 5136A0911E7599D900D4ED48 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5136A0901E7599D900D4ED48 /* ViewController.swift */; }; 12 | 5136A0941E7599D900D4ED48 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5136A0921E7599D900D4ED48 /* Main.storyboard */; }; 13 | 5136A0961E7599D900D4ED48 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5136A0951E7599D900D4ED48 /* Assets.xcassets */; }; 14 | 5136A0991E7599D900D4ED48 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5136A0971E7599D900D4ED48 /* LaunchScreen.storyboard */; }; 15 | 5136A0A11E759A2D00D4ED48 /* Tanzaku.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5136A0A01E759A2D00D4ED48 /* Tanzaku.swift */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | 5136A08B1E7599D900D4ED48 /* Tanzaku.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Tanzaku.app; sourceTree = BUILT_PRODUCTS_DIR; }; 20 | 5136A08E1E7599D900D4ED48 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 21 | 5136A0901E7599D900D4ED48 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 22 | 5136A0931E7599D900D4ED48 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 23 | 5136A0951E7599D900D4ED48 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 24 | 5136A0981E7599D900D4ED48 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 25 | 5136A09A1E7599D900D4ED48 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 26 | 5136A0A01E759A2D00D4ED48 /* Tanzaku.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Tanzaku.swift; sourceTree = ""; }; 27 | /* End PBXFileReference section */ 28 | 29 | /* Begin PBXFrameworksBuildPhase section */ 30 | 5136A0881E7599D900D4ED48 /* Frameworks */ = { 31 | isa = PBXFrameworksBuildPhase; 32 | buildActionMask = 2147483647; 33 | files = ( 34 | ); 35 | runOnlyForDeploymentPostprocessing = 0; 36 | }; 37 | /* End PBXFrameworksBuildPhase section */ 38 | 39 | /* Begin PBXGroup section */ 40 | 5136A0821E7599D800D4ED48 = { 41 | isa = PBXGroup; 42 | children = ( 43 | 5136A08D1E7599D900D4ED48 /* Tanzaku */, 44 | 5136A08C1E7599D900D4ED48 /* Products */, 45 | ); 46 | sourceTree = ""; 47 | }; 48 | 5136A08C1E7599D900D4ED48 /* Products */ = { 49 | isa = PBXGroup; 50 | children = ( 51 | 5136A08B1E7599D900D4ED48 /* Tanzaku.app */, 52 | ); 53 | name = Products; 54 | sourceTree = ""; 55 | }; 56 | 5136A08D1E7599D900D4ED48 /* Tanzaku */ = { 57 | isa = PBXGroup; 58 | children = ( 59 | 5136A0A01E759A2D00D4ED48 /* Tanzaku.swift */, 60 | 5136A08E1E7599D900D4ED48 /* AppDelegate.swift */, 61 | 5136A0901E7599D900D4ED48 /* ViewController.swift */, 62 | 5136A0921E7599D900D4ED48 /* Main.storyboard */, 63 | 5136A0951E7599D900D4ED48 /* Assets.xcassets */, 64 | 5136A0971E7599D900D4ED48 /* LaunchScreen.storyboard */, 65 | 5136A09A1E7599D900D4ED48 /* Info.plist */, 66 | ); 67 | path = Tanzaku; 68 | sourceTree = ""; 69 | }; 70 | /* End PBXGroup section */ 71 | 72 | /* Begin PBXNativeTarget section */ 73 | 5136A08A1E7599D900D4ED48 /* Tanzaku */ = { 74 | isa = PBXNativeTarget; 75 | buildConfigurationList = 5136A09D1E7599D900D4ED48 /* Build configuration list for PBXNativeTarget "Tanzaku" */; 76 | buildPhases = ( 77 | 5136A0871E7599D900D4ED48 /* Sources */, 78 | 5136A0881E7599D900D4ED48 /* Frameworks */, 79 | 5136A0891E7599D900D4ED48 /* Resources */, 80 | ); 81 | buildRules = ( 82 | ); 83 | dependencies = ( 84 | ); 85 | name = Tanzaku; 86 | productName = Tanzaku; 87 | productReference = 5136A08B1E7599D900D4ED48 /* Tanzaku.app */; 88 | productType = "com.apple.product-type.application"; 89 | }; 90 | /* End PBXNativeTarget section */ 91 | 92 | /* Begin PBXProject section */ 93 | 5136A0831E7599D900D4ED48 /* Project object */ = { 94 | isa = PBXProject; 95 | attributes = { 96 | LastSwiftUpdateCheck = 0820; 97 | LastUpgradeCheck = 0820; 98 | ORGANIZATIONNAME = usagimaru; 99 | TargetAttributes = { 100 | 5136A08A1E7599D900D4ED48 = { 101 | CreatedOnToolsVersion = 8.2; 102 | DevelopmentTeam = R6RZ6S2FLK; 103 | ProvisioningStyle = Automatic; 104 | }; 105 | }; 106 | }; 107 | buildConfigurationList = 5136A0861E7599D900D4ED48 /* Build configuration list for PBXProject "Tanzaku" */; 108 | compatibilityVersion = "Xcode 3.2"; 109 | developmentRegion = English; 110 | hasScannedForEncodings = 0; 111 | knownRegions = ( 112 | en, 113 | Base, 114 | ); 115 | mainGroup = 5136A0821E7599D800D4ED48; 116 | productRefGroup = 5136A08C1E7599D900D4ED48 /* Products */; 117 | projectDirPath = ""; 118 | projectRoot = ""; 119 | targets = ( 120 | 5136A08A1E7599D900D4ED48 /* Tanzaku */, 121 | ); 122 | }; 123 | /* End PBXProject section */ 124 | 125 | /* Begin PBXResourcesBuildPhase section */ 126 | 5136A0891E7599D900D4ED48 /* Resources */ = { 127 | isa = PBXResourcesBuildPhase; 128 | buildActionMask = 2147483647; 129 | files = ( 130 | 5136A0991E7599D900D4ED48 /* LaunchScreen.storyboard in Resources */, 131 | 5136A0961E7599D900D4ED48 /* Assets.xcassets in Resources */, 132 | 5136A0941E7599D900D4ED48 /* Main.storyboard in Resources */, 133 | ); 134 | runOnlyForDeploymentPostprocessing = 0; 135 | }; 136 | /* End PBXResourcesBuildPhase section */ 137 | 138 | /* Begin PBXSourcesBuildPhase section */ 139 | 5136A0871E7599D900D4ED48 /* Sources */ = { 140 | isa = PBXSourcesBuildPhase; 141 | buildActionMask = 2147483647; 142 | files = ( 143 | 5136A0911E7599D900D4ED48 /* ViewController.swift in Sources */, 144 | 5136A08F1E7599D900D4ED48 /* AppDelegate.swift in Sources */, 145 | 5136A0A11E759A2D00D4ED48 /* Tanzaku.swift in Sources */, 146 | ); 147 | runOnlyForDeploymentPostprocessing = 0; 148 | }; 149 | /* End PBXSourcesBuildPhase section */ 150 | 151 | /* Begin PBXVariantGroup section */ 152 | 5136A0921E7599D900D4ED48 /* Main.storyboard */ = { 153 | isa = PBXVariantGroup; 154 | children = ( 155 | 5136A0931E7599D900D4ED48 /* Base */, 156 | ); 157 | name = Main.storyboard; 158 | sourceTree = ""; 159 | }; 160 | 5136A0971E7599D900D4ED48 /* LaunchScreen.storyboard */ = { 161 | isa = PBXVariantGroup; 162 | children = ( 163 | 5136A0981E7599D900D4ED48 /* Base */, 164 | ); 165 | name = LaunchScreen.storyboard; 166 | sourceTree = ""; 167 | }; 168 | /* End PBXVariantGroup section */ 169 | 170 | /* Begin XCBuildConfiguration section */ 171 | 5136A09B1E7599D900D4ED48 /* Debug */ = { 172 | isa = XCBuildConfiguration; 173 | buildSettings = { 174 | ALWAYS_SEARCH_USER_PATHS = NO; 175 | CLANG_ANALYZER_NONNULL = YES; 176 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 177 | CLANG_CXX_LIBRARY = "libc++"; 178 | CLANG_ENABLE_MODULES = YES; 179 | CLANG_ENABLE_OBJC_ARC = YES; 180 | CLANG_WARN_BOOL_CONVERSION = YES; 181 | CLANG_WARN_CONSTANT_CONVERSION = YES; 182 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 183 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 184 | CLANG_WARN_EMPTY_BODY = YES; 185 | CLANG_WARN_ENUM_CONVERSION = YES; 186 | CLANG_WARN_INFINITE_RECURSION = YES; 187 | CLANG_WARN_INT_CONVERSION = YES; 188 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 189 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 190 | CLANG_WARN_UNREACHABLE_CODE = YES; 191 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 192 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 193 | COPY_PHASE_STRIP = NO; 194 | DEBUG_INFORMATION_FORMAT = dwarf; 195 | ENABLE_STRICT_OBJC_MSGSEND = YES; 196 | ENABLE_TESTABILITY = YES; 197 | GCC_C_LANGUAGE_STANDARD = gnu99; 198 | GCC_DYNAMIC_NO_PIC = NO; 199 | GCC_NO_COMMON_BLOCKS = YES; 200 | GCC_OPTIMIZATION_LEVEL = 0; 201 | GCC_PREPROCESSOR_DEFINITIONS = ( 202 | "DEBUG=1", 203 | "$(inherited)", 204 | ); 205 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 206 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 207 | GCC_WARN_UNDECLARED_SELECTOR = YES; 208 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 209 | GCC_WARN_UNUSED_FUNCTION = YES; 210 | GCC_WARN_UNUSED_VARIABLE = YES; 211 | IPHONEOS_DEPLOYMENT_TARGET = 10.2; 212 | MTL_ENABLE_DEBUG_INFO = YES; 213 | ONLY_ACTIVE_ARCH = YES; 214 | SDKROOT = iphoneos; 215 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 216 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 217 | }; 218 | name = Debug; 219 | }; 220 | 5136A09C1E7599D900D4ED48 /* Release */ = { 221 | isa = XCBuildConfiguration; 222 | buildSettings = { 223 | ALWAYS_SEARCH_USER_PATHS = NO; 224 | CLANG_ANALYZER_NONNULL = YES; 225 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 226 | CLANG_CXX_LIBRARY = "libc++"; 227 | CLANG_ENABLE_MODULES = YES; 228 | CLANG_ENABLE_OBJC_ARC = YES; 229 | CLANG_WARN_BOOL_CONVERSION = YES; 230 | CLANG_WARN_CONSTANT_CONVERSION = YES; 231 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 232 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 233 | CLANG_WARN_EMPTY_BODY = YES; 234 | CLANG_WARN_ENUM_CONVERSION = YES; 235 | CLANG_WARN_INFINITE_RECURSION = YES; 236 | CLANG_WARN_INT_CONVERSION = YES; 237 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 238 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 239 | CLANG_WARN_UNREACHABLE_CODE = YES; 240 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 241 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 242 | COPY_PHASE_STRIP = NO; 243 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 244 | ENABLE_NS_ASSERTIONS = NO; 245 | ENABLE_STRICT_OBJC_MSGSEND = YES; 246 | GCC_C_LANGUAGE_STANDARD = gnu99; 247 | GCC_NO_COMMON_BLOCKS = YES; 248 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 249 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 250 | GCC_WARN_UNDECLARED_SELECTOR = YES; 251 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 252 | GCC_WARN_UNUSED_FUNCTION = YES; 253 | GCC_WARN_UNUSED_VARIABLE = YES; 254 | IPHONEOS_DEPLOYMENT_TARGET = 10.2; 255 | MTL_ENABLE_DEBUG_INFO = NO; 256 | SDKROOT = iphoneos; 257 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 258 | VALIDATE_PRODUCT = YES; 259 | }; 260 | name = Release; 261 | }; 262 | 5136A09E1E7599D900D4ED48 /* Debug */ = { 263 | isa = XCBuildConfiguration; 264 | buildSettings = { 265 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 266 | DEVELOPMENT_TEAM = R6RZ6S2FLK; 267 | INFOPLIST_FILE = Tanzaku/Info.plist; 268 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 269 | PRODUCT_BUNDLE_IDENTIFIER = jp.usagimaru.Tanzaku; 270 | PRODUCT_NAME = "$(TARGET_NAME)"; 271 | SWIFT_VERSION = 3.0; 272 | }; 273 | name = Debug; 274 | }; 275 | 5136A09F1E7599D900D4ED48 /* Release */ = { 276 | isa = XCBuildConfiguration; 277 | buildSettings = { 278 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 279 | DEVELOPMENT_TEAM = R6RZ6S2FLK; 280 | INFOPLIST_FILE = Tanzaku/Info.plist; 281 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 282 | PRODUCT_BUNDLE_IDENTIFIER = jp.usagimaru.Tanzaku; 283 | PRODUCT_NAME = "$(TARGET_NAME)"; 284 | SWIFT_VERSION = 3.0; 285 | }; 286 | name = Release; 287 | }; 288 | /* End XCBuildConfiguration section */ 289 | 290 | /* Begin XCConfigurationList section */ 291 | 5136A0861E7599D900D4ED48 /* Build configuration list for PBXProject "Tanzaku" */ = { 292 | isa = XCConfigurationList; 293 | buildConfigurations = ( 294 | 5136A09B1E7599D900D4ED48 /* Debug */, 295 | 5136A09C1E7599D900D4ED48 /* Release */, 296 | ); 297 | defaultConfigurationIsVisible = 0; 298 | defaultConfigurationName = Release; 299 | }; 300 | 5136A09D1E7599D900D4ED48 /* Build configuration list for PBXNativeTarget "Tanzaku" */ = { 301 | isa = XCConfigurationList; 302 | buildConfigurations = ( 303 | 5136A09E1E7599D900D4ED48 /* Debug */, 304 | 5136A09F1E7599D900D4ED48 /* Release */, 305 | ); 306 | defaultConfigurationIsVisible = 0; 307 | defaultConfigurationName = Release; 308 | }; 309 | /* End XCConfigurationList section */ 310 | }; 311 | rootObject = 5136A0831E7599D900D4ED48 /* Project object */; 312 | } 313 | -------------------------------------------------------------------------------- /Tanzaku/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 53 | 59 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 90 | 96 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | -------------------------------------------------------------------------------- /Tanzaku/Tanzaku.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Tanzaku 3 | // 4 | // Created by usagimaru on 2016.12.14. 5 | // Copyright © 2016 usagimaru. All rights reserved. 6 | // 7 | 8 | import UIKit 9 | 10 | /// 縦書き和文を描画する短冊 11 | class Tanzaku: UIView { 12 | 13 | /// 文字列 14 | @IBInspectable var text: String? { 15 | didSet { 16 | update() 17 | } 18 | } 19 | 20 | /// フォント名 21 | @IBInspectable var fontFamily: String = UIFont.systemFont(ofSize: 0).familyName { 22 | didSet { 23 | if let f = UIFont(name: fontFamily, size: fontSize) { 24 | font = f 25 | } 26 | else { 27 | update() 28 | } 29 | } 30 | } 31 | 32 | /// フォントサイズ 33 | @IBInspectable var fontSize: CGFloat = kDefaultFontSize { 34 | didSet { 35 | if let f = UIFont(name: fontFamily, size: fontSize) { 36 | font = f 37 | } 38 | else { 39 | update() 40 | } 41 | } 42 | } 43 | 44 | /// フォント 45 | var font: UIFont = UIFont.systemFont(ofSize: kDefaultFontSize) { 46 | didSet { 47 | update() 48 | } 49 | } 50 | 51 | /// 文字色 52 | @IBInspectable var textColor: UIColor = UIColor.black { 53 | didSet { 54 | update() 55 | } 56 | } 57 | 58 | /// 行数 59 | @IBInspectable var numberOfLines: UInt = 0 { 60 | didSet { 61 | update() 62 | } 63 | } 64 | 65 | /// 行間 66 | @IBInspectable var lineSpacing: CGFloat = 0 { 67 | didSet { 68 | update() 69 | } 70 | } 71 | 72 | /// 配置方法 73 | enum TextAlignment: Int { 74 | /// 上配置 75 | case top 76 | /// 中央配置 77 | case center 78 | /// 下配置 79 | case bottom 80 | 81 | static func convert(from nativeTextAlignment: NSTextAlignment) -> TextAlignment { 82 | switch nativeTextAlignment { 83 | case .center: 84 | return TextAlignment.center 85 | case .right: 86 | return TextAlignment.bottom 87 | default: 88 | return TextAlignment.top 89 | } 90 | } 91 | 92 | func convertToNativeTextAlignment() -> NSTextAlignment { 93 | switch self { 94 | case .top: 95 | return .left 96 | case .center: 97 | return .center 98 | case .bottom: 99 | return .right 100 | } 101 | } 102 | } 103 | 104 | var textAlignment: TextAlignment = .top { 105 | didSet { 106 | update() 107 | } 108 | } 109 | 110 | /// 省略方法 111 | enum TruncationMode: Int { 112 | /// 文字単位折り返し 113 | case byCharWrapping 114 | /// 上略 115 | case byTruncatingHead 116 | /// 中略 117 | case byTruncatingMiddle 118 | /// 下略 119 | case byTruncatingTail 120 | /// 均等割付 121 | case byJustification 122 | 123 | /// NSLineBreakMode -> TruncationMode 124 | static func convert(from nativeLineBreakMode: NSLineBreakMode) -> TruncationMode { 125 | switch nativeLineBreakMode { 126 | case .byTruncatingHead: 127 | return TruncationMode.byTruncatingHead 128 | case .byTruncatingMiddle: 129 | return TruncationMode.byTruncatingMiddle 130 | case .byTruncatingTail: 131 | return TruncationMode.byTruncatingTail 132 | default: 133 | return TruncationMode.byCharWrapping 134 | } 135 | } 136 | 137 | /// TruncationMode -> NSLineBreakMode 138 | func convertToNativeLineBreakMode() -> NSLineBreakMode { 139 | switch self { 140 | case .byTruncatingHead: 141 | return NSLineBreakMode.byTruncatingHead 142 | case .byTruncatingMiddle: 143 | return NSLineBreakMode.byTruncatingMiddle 144 | case .byTruncatingTail: 145 | return NSLineBreakMode.byTruncatingTail 146 | default: 147 | return NSLineBreakMode.byCharWrapping 148 | } 149 | } 150 | 151 | /// TruncationMode -> CTLineBreakMode 152 | func convertToCoreTextLineBreakMode() -> CTLineBreakMode { 153 | switch self { 154 | case .byTruncatingHead: 155 | return CTLineBreakMode.byTruncatingHead 156 | case .byTruncatingMiddle: 157 | return CTLineBreakMode.byTruncatingMiddle 158 | case .byTruncatingTail: 159 | return CTLineBreakMode.byTruncatingTail 160 | default: 161 | return CTLineBreakMode.byCharWrapping 162 | } 163 | } 164 | 165 | /// TruncationMode -> CTLineTruncationType 166 | func lineTruncationType() -> CTLineTruncationType? { 167 | switch self { 168 | case .byTruncatingHead: 169 | return CTLineTruncationType.start 170 | case .byTruncatingMiddle: 171 | return CTLineTruncationType.middle 172 | case .byTruncatingTail: 173 | return CTLineTruncationType.end 174 | default: 175 | return nil 176 | } 177 | } 178 | 179 | /// Head OR Middle OR Tail 180 | func isTruncation() -> Bool { 181 | switch self { 182 | case .byTruncatingHead, .byTruncatingMiddle, .byTruncatingTail: 183 | return true 184 | default: 185 | return false 186 | } 187 | } 188 | } 189 | 190 | /// 省略方法 191 | var truncationMode: TruncationMode = .byCharWrapping { 192 | didSet { 193 | update() 194 | } 195 | } 196 | 197 | 198 | // MARK: - Private Properties 199 | 200 | private static let kDefaultFontSize = CGFloat(17.0) 201 | 202 | private var truncationToken: CTLine? 203 | private var textInfo: TextInfo? 204 | 205 | 206 | // MARK: - 207 | 208 | /// 属性付き文字列を作る 209 | private class func createAttributedString(_ text: String, 210 | font: UIFont, 211 | lineSpacing: CGFloat = 0, 212 | textAlignment: TextAlignment, 213 | textColor: UIColor? = nil) -> NSAttributedString { 214 | let lineHeight = (font.lineHeight - font.descender) 215 | 216 | let paragraph = NSMutableParagraphStyle() 217 | paragraph.lineSpacing = lineSpacing 218 | paragraph.minimumLineHeight = lineHeight 219 | paragraph.maximumLineHeight = lineHeight 220 | paragraph.lineBreakMode = .byCharWrapping 221 | paragraph.alignment = textAlignment.convertToNativeTextAlignment() 222 | 223 | var attributes = [ 224 | NSFontAttributeName : font, 225 | NSParagraphStyleAttributeName : paragraph, 226 | kCTVerticalFormsAttributeName as String : true , 227 | ] as [String : Any] 228 | if let textColor = textColor { 229 | attributes[NSForegroundColorAttributeName] = textColor 230 | } 231 | 232 | let attributedText = NSMutableAttributedString(string: text, attributes: attributes) 233 | 234 | return attributedText as NSAttributedString 235 | } 236 | 237 | /// テキスト情報 238 | private struct TextInfo { 239 | let framesetter: CTFramesetter 240 | let frameSize: CGSize 241 | let fitRange: CFRange 242 | let lineHeight: CGFloat 243 | let lineSpacing: CGFloat 244 | let textLength: Int 245 | let fullLine: CTLine? 246 | let attributedString: NSAttributedString? 247 | 248 | var NSFitRange: NSRange { 249 | return NSRange(location: fitRange.location, length: fitRange.length) 250 | } 251 | } 252 | 253 | /// テキスト情報を取得 254 | private class func textInfo(_ text: String, 255 | rect: CGRect, 256 | font: UIFont, 257 | numberOfLines: UInt, 258 | lineSpacing: CGFloat, 259 | textAlignment: TextAlignment, 260 | textColor: UIColor? = nil, 261 | containsFullLine: Bool = false) -> TextInfo? { 262 | let attributedString = createAttributedString(text, 263 | font: font, 264 | lineSpacing: lineSpacing, 265 | textAlignment: textAlignment, 266 | textColor: textColor) 267 | let length = attributedString.length 268 | let framesetter = CTFramesetterCreateWithAttributedString(attributedString as CFAttributedString) 269 | 270 | // サイズ情報を取得 271 | var fitRange = CFRange() 272 | var size = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, 273 | CFRange(location: 0, length: 0), 274 | Tanzaku.framesetterOptions(), 275 | rect.size, 276 | &fitRange) 277 | // 正しい縦幅に補正 278 | size.width = ceil(size.width + font.leading) + 1 279 | 280 | // 行高 281 | let lineHeight = (font.lineHeight - font.descender) 282 | // MEMO: 283 | // LINE HEIGHT = font.lineHeight - font.descender 284 | // font.lineHeight == leading * 2 285 | // font.descender * 2 == (font.ascender - font.lineHeight + font.descender) 286 | 287 | 288 | // 最大行数に合わせた横幅 289 | if numberOfLines > 0 { 290 | let suggestedWidth = lineHeight * CGFloat(numberOfLines) + lineSpacing * CGFloat(numberOfLines - 1) 291 | if size.width > suggestedWidth { 292 | size.width = suggestedWidth + 1 293 | } 294 | } 295 | 296 | 297 | // 全体の CTLine オブジェクトが必要なら用意 298 | let fullLine: CTLine? = containsFullLine ? CTLineCreateWithAttributedString(attributedString) : nil 299 | 300 | 301 | return TextInfo(framesetter: framesetter, 302 | frameSize: size, 303 | fitRange: fitRange, 304 | lineHeight: lineHeight, 305 | lineSpacing: lineSpacing, 306 | textLength: length, 307 | fullLine: fullLine, 308 | attributedString: attributedString) 309 | } 310 | 311 | 312 | // MARK: - 描画 313 | 314 | /// 再描画 315 | private func update() { 316 | // Intrinsic Content Size をリセット 317 | invalidateIntrinsicContentSize() 318 | 319 | // 省略文字を準備 320 | // 縦書きでも U+2026 で問題なさそう 321 | // U+2026…(HORIZONTAL ELLIPSIS) 322 | // U+FE19︙(PRESENTATION FORM FOR VERTICAL HORIZONTAL ELLIPSIS) 323 | let token = "…" 324 | let tokenText = Tanzaku.createAttributedString( 325 | token, 326 | font: font, 327 | lineSpacing: lineSpacing, 328 | textAlignment: textAlignment, 329 | textColor: textColor) as CFAttributedString 330 | truncationToken = CTLineCreateWithAttributedString(tokenText) 331 | 332 | // 再描画 333 | setNeedsDisplay() 334 | } 335 | 336 | /// 行を描画 337 | private func drawLines(in rect: CGRect, context: CGContext) { 338 | guard let text = text else { 339 | context.clear(rect) 340 | return 341 | } 342 | guard let textInfo = Tanzaku.textInfo( 343 | text, 344 | rect: rect, 345 | font: font, 346 | numberOfLines: numberOfLines, 347 | lineSpacing: lineSpacing, 348 | textAlignment: textAlignment, 349 | textColor: textColor, 350 | containsFullLine: true) 351 | else {return} 352 | self.textInfo = textInfo 353 | 354 | #if DEBUG_LINES 355 | print("\n\nLine Height\(font.lineHeight - font.descender), lineHeight: \(font.lineHeight), pointSize: \(font.pointSize), lineSpacing: \(textInfo.lineSpacing),\nascender: \(font.ascender), descender: \(font.descender), leading: \(font.leading)") 356 | #endif 357 | 358 | let framesetter = textInfo.framesetter 359 | let frame = CTFramesetterCreateFrame(framesetter, CFRange(location: 0, length: 0), CGPath(rect: rect, transform: nil), Tanzaku.framesetterOptions()) 360 | let lines = CTFrameGetLines(frame) as! [CTLine] 361 | let maxLineCount = numberOfLines > 0 ? min(Int(numberOfLines), lines.count) : lines.count 362 | var translate_x = (rect.width - textInfo.lineHeight * CGFloat(maxLineCount)) / 2.0 // 行全体をビューの横中央配置にするための調整 363 | if maxLineCount > 1 { 364 | translate_x -= (textInfo.lineSpacing * CGFloat(maxLineCount - 1)) / 2 365 | } 366 | 367 | context.saveGState() 368 | 369 | context.scaleBy(x: 1.0, y: -1.0) 370 | context.translateBy(x: font.descender * 2 - translate_x, 371 | y: -rect.height - font.descender / 2) 372 | context.rotate(by: CGFloat(-M_PI_2)) 373 | 374 | 375 | // 各行を描画 376 | for (i, line) in lines.enumerated() { 377 | var thisLine: CTLine = line 378 | 379 | // 行数制限を考慮 380 | if numberOfLines != 0 && i >= Int(numberOfLines) { 381 | break 382 | } 383 | 384 | // 行の縦幅 385 | let lineWidth = CTLineGetBoundsWithOptions(thisLine, Tanzaku.lineBoundsOptions()).width 386 | 387 | // 均等割付 388 | if truncationMode == .byJustification { 389 | let justificationWidth = Double(bounds.height) // 行の幅 390 | if let justifiedLine = justifiedLine(thisLine, justificationWidth: justificationWidth) { 391 | thisLine = justifiedLine 392 | } 393 | } 394 | 395 | // 省略処理:上略・中略・下略 396 | // 条件:最大行数目に到達 AND 行の終端部分の文字インデックスが文字数未満 397 | if truncationMode.isTruncation() && 398 | i >= Int(maxLineCount) - 1 && 399 | CTLineGetStringIndexForPosition(thisLine, CGPoint(x: lineWidth, y: 0)) < textInfo.textLength { 400 | if let truncatedLine = truncatedLine(thisLine, truncationType: truncationMode.lineTruncationType()!) { 401 | thisLine = truncatedLine 402 | } 403 | } 404 | 405 | // 行を描画 406 | drawLine(thisLine, atIndex: i, withLineHeight: textInfo.lineHeight, inRect: rect, toContext: context) 407 | } 408 | 409 | context.restoreGState() 410 | } 411 | 412 | 413 | // 行を描画 414 | private func drawLine(_ line: CTLine, 415 | atIndex lineIndex: CFIndex, 416 | withLineHeight lineHeight: CGFloat, 417 | inRect rect: CGRect, 418 | toContext context: CGContext) { 419 | // MEMO: 420 | // lineRect.y == -lineHeight 421 | // lineRect.width == lineRect.height * 2 422 | // font.descender == lineRect.y + font.ascender 423 | 424 | let lineBounds = CTLineGetBoundsWithOptions(line, Tanzaku.lineBoundsOptions()) 425 | let lineSpacing = self.lineSpacing * CGFloat(lineIndex) 426 | let x = rect.width - lineHeight * CGFloat(lineIndex) + font.descender * 2 + font.descender / 2 - lineSpacing 427 | var y = -rect.height - font.descender / 2 428 | 429 | // 配置 430 | switch textAlignment { 431 | case .center: 432 | y += (rect.height - lineBounds.width) / 2 433 | break 434 | case .bottom: 435 | y += (rect.height - lineBounds.width) 436 | break 437 | default: 438 | break 439 | } 440 | 441 | context.textPosition = CGPoint(x: y, y: x) 442 | CTLineDraw(line, context) 443 | 444 | #if DEBUG_LINES 445 | // 行の枠線を描画 446 | context.saveGState() 447 | 448 | let fx = rect.width - lineHeight * CGFloat(lineIndex + 1) - font.descender * 2 - lineSpacing 449 | let fw = lineHeight 450 | let fh = lineBounds.width 451 | let frameRect = CGRect(x: y, y: fx, width: fh, height: fw) 452 | 453 | context.addRect(frameRect) 454 | context.setStrokeColor(UIColor.orange.cgColor) 455 | context.setLineWidth(1.0) 456 | context.strokePath() 457 | 458 | context.restoreGState() 459 | #endif 460 | } 461 | 462 | /// 省略処理 463 | private func truncatedLine(_ targetLine: CTLine, truncationType: CTLineTruncationType) -> CTLine? { 464 | // 行の縦幅 465 | let lineWidth = CTLineGetBoundsWithOptions(targetLine, Tanzaku.lineBoundsOptions()).width 466 | 467 | switch truncationMode { 468 | case .byTruncatingHead: 469 | // 上略 470 | 471 | if let fullLine = textInfo?.fullLine, 472 | let truncatedLine = CTLineCreateTruncatedLine(fullLine, Double(lineWidth), .start, truncationToken) { 473 | return truncatedLine 474 | } 475 | case .byTruncatingMiddle, .byTruncatingTail: 476 | // 中略・下略 477 | 478 | guard let attributedString = textInfo?.attributedString else {break} 479 | let truncation = truncationMode.lineTruncationType()! // CTLineTruncationType 480 | let startIndex = CTLineGetStringIndexForPosition(targetLine, CGPoint(x: 0, y: 0)) 481 | let substring = attributedString.attributedSubstring(from: NSRange(location: startIndex, length: attributedString.length - startIndex)) 482 | let recreatedLine = CTLineCreateWithAttributedString(substring) 483 | 484 | if let truncatedLine = CTLineCreateTruncatedLine(recreatedLine, Double(lineWidth), truncation, truncationToken) { 485 | return truncatedLine 486 | } 487 | default: 488 | return nil 489 | } 490 | return nil 491 | } 492 | 493 | /// 均等割付 494 | private func justifiedLine(_ targetLine: CTLine, justificationWidth: Double) -> CTLine? { 495 | let justificationFactor = CGFloat(1.0) // 割付率:1.0以上なら完全な均等割付、未満なら部分的な均等割付処理を適用する。0.0以下は何もしない 496 | return CTLineCreateJustifiedLine(targetLine, justificationFactor, justificationWidth) 497 | } 498 | 499 | override func draw(_ rect: CGRect) { 500 | super.draw(rect) 501 | guard let context = UIGraphicsGetCurrentContext() else {return} 502 | 503 | drawLines(in: rect, context: context) 504 | } 505 | 506 | 507 | // MARK: - サイズ計算 508 | 509 | override public var intrinsicContentSize: CGSize { 510 | return prefferedTextSize(bounds.height) 511 | } 512 | 513 | override func sizeThatFits(_ size: CGSize) -> CGSize { 514 | return prefferedTextSize(size.height) 515 | } 516 | 517 | class func prefferedTextSize(_ text: String, 518 | font: UIFont, 519 | numberOfLines: UInt = 0, 520 | lineSpacing: CGFloat, 521 | textAlignment: TextAlignment, 522 | constraintHeight: CGFloat) -> CGSize { 523 | let rect = CGRect(x: 0, y: 0, width: CGFloat.greatestFiniteMagnitude, height: constraintHeight) 524 | if let size = textInfo(text, 525 | rect: rect, 526 | font: font, 527 | numberOfLines: numberOfLines, 528 | lineSpacing: lineSpacing, 529 | textAlignment: textAlignment)?.frameSize { 530 | return size 531 | } 532 | 533 | return CGSize.zero 534 | } 535 | 536 | func prefferedTextSize(_ constraintHeight: CGFloat) -> CGSize { 537 | if let text = text { 538 | let size = Tanzaku.prefferedTextSize( 539 | text, 540 | font: font, 541 | numberOfLines: numberOfLines, 542 | lineSpacing: lineSpacing, 543 | textAlignment: textAlignment, 544 | constraintHeight: constraintHeight) 545 | return size 546 | } 547 | 548 | return CGSize.zero 549 | } 550 | 551 | } 552 | 553 | 554 | // MARK: - 共通のオプション 555 | 556 | extension Tanzaku { 557 | 558 | fileprivate static func framesetterOptions() -> CFDictionary { 559 | return [ 560 | kCTFrameProgressionAttributeName as String : CTFrameProgression.rightToLeft.rawValue, 561 | ] as CFDictionary 562 | } 563 | 564 | fileprivate static func lineBoundsOptions() -> CTLineBoundsOptions { 565 | return [.useOpticalBounds] 566 | } 567 | 568 | } 569 | --------------------------------------------------------------------------------