Latest 1.0.5
Homepage https://github.com/pcjbird/fbCharm
License MIT
Platforms ios 8.0, requires ARC
Frameworks Foundation, UIKit
Authors

logo
Build Status
Pod Version
Pod Platform
Pod License
CocoaPods
CocoaPods
GitHub release
GitHub release
Website

fbCharm (Charm of Fallback)

iOS 上利用 fallback 机制为不同语言的文字 (script) 设定字体,从而使得文本混排更为优雅。

如果您是一位 WEB 开发者,相信您对 CSS 的 font-family 属性一定不会陌生。通常我们会为 font-family 属性设置一长串的字体(家族)列表,就像这样的:

.text { font-family: Menlo, Monaco, Consolas, "Courier New", monospace, Arial, "Microsoft YaHei", "黑体", "宋体", sans-serif; }

有人肯定会问,为什么要这么设置啊?如果你足够细心,你一定会发现当你浏览该网页时英文字体和中文字体并不是同一种字体,如果你在不同的操作系统甚至不同的电脑上看到网页所呈现的字体也不一样。这又是为什么呢?

此外,在您使用 Microsoft Word 进行段落排版的时候,你会发现 Word 可以自动对中文样式应用中文字体,对英文样式应用英文字体。这又是如何做到的呢?

在实际的排版需求中,为了好看,我们通常会需要针对不同语言的文字 (script) 进行不同的字体设定,以达到最佳的视觉效果。当然,对于我们来说,最常见的还是中英文混排。通常设计人员给出的设计稿非常漂亮,可是中英文使用了不同的字体,我们开发人员该如何高保真地还原设计稿的原始设计呢?

很显然,对于 WEB 开发者来说,已经有了很好的解决方案,而对于其他客户端的同学来说,这估计应该就有点犯难了,我们通常会告诉设计人员:“系统不支持” 抑或 “做不了” 之类的话。那事实上,到底能不能做到呢?答案是肯定的。

特性 / Features

  1. 支持类似 CSS 的 font-family 属性一样设置一个字体家族 (font-family) 列表或字体名称 (font-name) 列表从而创建一个混合字体 (UIFont)。
  2. 支持同样的方式创建带样式的NSAttributedString,支持设置文本间隔 (kern), 字重 (weight) 等。
  3. 支持 fallback 到指定的系统字体。

演示 / Demo

安装 / Installation

方法一:fbCharm is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'fbCharm'

使用 / Usage

let text = "これは日本語文章と Roman Text の混植文章です。美しいヒラギノと San Francisco で日本語とローマ字を書きます。System Font のフォントメトリクスには独自の調整が入っています。nn这是一段中文简体。n這是一段中文繁體。nうつくしい森で飾られたモーリオ市、n郊外のぎらぎらひかる草の波。n祇辻飴葛蛸鯖鰯噌庖箸n底辺直卿蝕薩化nABCDEFGHIJKLMnabcdefghijklmn1234567890niClockᴹᴵᴺᴵnClockª"
let fontSize: CGFloat = 22.0

// 適当に行間を空ける処理
let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
paragraphStyle.minimumLineHeight = fontSize * 1.5
paragraphStyle.maximumLineHeight = paragraphStyle.minimumLineHeight

let attributedString = NSMutableAttributedString(text: text, families: ["Lucida Grande", "Baskerville", "Apple SD Gothic Neo"], size: 18.0, weight: .light, kern: 4.0, paragraphStyle: paragraphStyle)
self.label.attributedText = attributedString

//let font = UIFont(families: ["Lucida Grande", "Baskerville", "Apple SD Gothic Neo"], size: 20, weight: .medium)
//self.label.font = font
//self.label.text = text

关注我们 / Follow us

  
  
  
  Twitter URL
  Twitter Follow

Latest podspec

{
    "name": "fbCharm",
    "version": "1.0.5",
    "summary": "iOS u4e0au5229u7528 fallback u673au5236u4e3au4e0du540cu8bedu8a00u7684u6587u5b57 (script) u8bbeu5b9au5b57u4f53uff0cu4eceu800cu4f7fu5f97u6587u672cu6df7u6392u66f4u4e3au4f18u96c5u3002",
    "description": "iOS u4e0au5229u7528 fallback u673au5236u4e3au4e0du540cu8bedu8a00u7684u6587u5b57 (script) u8bbeu5b9au5b57u4f53uff0cu4eceu800cu4f7fu5f97u6587u672cu6df7u6392u66f4u4e3au4f18u96c5u3002nnu5982u679cu60a8u662fu4e00u4f4d WEB u5f00u53d1u8005uff0cu76f8u4fe1u60a8u5bf9 CSS u7684 font-family u5c5eu6027u4e00u5b9au4e0du4f1au964cu751fu3002u901au5e38u6211u4eecu4f1au4e3a font-family u5c5eu6027u8bbeu7f6eu4e00u957fu4e32u7684u5b57u4f53(u5bb6u65cf)u5217u8868uff0cu5c31u50cfu8fd9u6837u7684uff1ann.text { font-family: Menlo, Monaco, Consolas, "Courier New", monospace, Arial, "Microsoft YaHei", "u9ed1u4f53", "u5b8bu4f53", sans-serif; }nnu6709u4ebau80afu5b9au4f1au95eeuff0cu4e3au4ec0u4e48u8981u8fd9u4e48u8bbeu7f6eu554auff1fu5982u679cu4f60u8db3u591fu7ec6u5fc3uff0cu4f60u4e00u5b9au4f1au53d1u73b0u5f53u4f60u6d4fu89c8u8be5u7f51u9875u65f6u82f1u6587u5b57u4f53u548cu4e2du6587u5b57u4f53u5e76u4e0du662fu540cu4e00u79cdu5b57u4f53uff0cu5982u679cu4f60u5728u4e0du540cu7684u64cdu4f5cu7cfbu7edfu751au81f3u4e0du540cu7684u7535u8111u4e0au770bu5230u7f51u9875u6240u5448u73b0u7684u5b57u4f53u4e5fu4e0du4e00u6837u3002u8fd9u53c8u662fu4e3au4ec0u4e48u5462uff1fnnu6b64u5916uff0cu5728u60a8u4f7fu7528 Microsoft Word u8fdbu884cu6bb5u843du6392u7248u7684u65f6u5019uff0cu4f60u4f1au53d1u73b0 Word u53efu4ee5u81eau52a8u5bf9u4e2du6587u6837u5f0fu5e94u7528u4e2du6587u5b57u4f53uff0cu5bf9u82f1u6587u6837u5f0fu5e94u7528u82f1u6587u5b57u4f53u3002u8fd9u53c8u662fu5982u4f55u505au5230u7684u5462uff1fnnu5728u5b9eu9645u7684u6392u7248u9700u6c42u4e2duff0cu4e3au4e86u597du770buff0cu6211u4eecu901au5e38u4f1au9700u8981u9488u5bf9u4e0du540cu8bedu8a00u7684u6587u5b57 (script) u8fdbu884cu4e0du540cu7684u5b57u4f53u8bbeu5b9auff0cu4ee5u8fbeu5230u6700u4f73u7684u89c6u89c9u6548u679cu3002u5f53u7136uff0cu5bf9u4e8eu6211u4eecu6765u8bf4uff0cu6700u5e38u89c1u7684u8fd8u662fu4e2du82f1u6587u6df7u6392u3002u901au5e38u8bbeu8ba1u4ebau5458u7ed9u51fau7684u8bbeu8ba1u7a3fu975eu5e38u6f02u4eaeuff0cu53efu662fu4e2du82f1u6587u4f7fu7528u4e86u4e0du540cu7684u5b57u4f53uff0cu6211u4eecu5f00u53d1u4ebau5458u8be5u5982u4f55u9ad8u4fddu771fu5730u8fd8u539fu8bbeu8ba1u7a3fu7684u539fu59cbu8bbeu8ba1u5462uff1fnnu5f88u663eu7136uff0cu5bf9u4e8e WEB u5f00u53d1u8005u6765u8bf4uff0cu5df2u7ecfu6709u4e86u5f88u597du7684u89e3u51b3u65b9u6848uff0cu800cu5bf9u4e8eu5176u4ed6u5ba2u6237u7aefu7684u540cu5b66u6765u8bf4uff0cu8fd9u4f30u8ba1u5e94u8be5u5c31u6709u70b9u72afu96beu4e86uff0cu6211u4eecu901au5e38u4f1au544au8bc9u8bbeu8ba1u4ebau5458uff1au201cu7cfbu7edfu4e0du652fu6301u201d u6291u6216 u201cu505au4e0du4e86u201d u4e4bu7c7bu7684u8bddu3002u90a3u4e8bu5b9eu4e0auff0cu5230u5e95u80fdu4e0du80fdu505au5230u5462uff1fu7b54u6848u662fu80afu5b9au7684u3002",
    "homepage": "https://github.com/pcjbird/fbCharm",
    "license": "MIT",
    "authors": {
        "pcjbird": "[email protected]"
    },
    "source": {
        "git": "https://github.com/pcjbird/fbCharm.git",
        "tag": "1.0.5"
    },
    "social_media_url": "http://www.lessney.com",
    "requires_arc": true,
    "documentation_url": "https://github.com/pcjbird/fbCharm/blob/master/README.md",
    "screenshots": "https://github.com/pcjbird/fbCharm/raw/master/logo.png",
    "platforms": {
        "ios": "8.0"
    },
    "frameworks": [
        "Foundation",
        "UIKit"
    ],
    "source_files": "fbCharm/*.swift",
    "pod_target_xcconfig": {
        "OTHER_LDFLAGS": "-lObjC"
    }
}

Pin It on Pinterest

Share This