Skip to content

Commit

Permalink
Merge pull request #20 from kazuhiro4949/feature/attachment
Browse files Browse the repository at this point in the history
Utilize NSTextAttachment
  • Loading branch information
kazuhiro4949 committed Mar 21, 2018
2 parents 4e64568 + 903f424 commit a52a1f0
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 4 deletions.
6 changes: 6 additions & 0 deletions StringStylizer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
2B3A7F8020629F1A008C4ED9 /* UIImage+StringStylizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B3A7F7F20629F1A008C4ED9 /* UIImage+StringStylizer.swift */; };
2B3A7F8120629F24008C4ED9 /* UIImage+StringStylizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B3A7F7F20629F1A008C4ED9 /* UIImage+StringStylizer.swift */; };
2BE7A29420216C650039610C /* StringStylizer.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2BE7A28B20216C650039610C /* StringStylizer.framework */; };
2BE7A2A220216C980039610C /* StringStylizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E3F299A1CB2AB3200702DD3 /* StringStylizer.swift */; };
2BE7A2A320216C980039610C /* StringStylizerFontName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E3F299B1CB2AB3200702DD3 /* StringStylizerFontName.swift */; };
Expand Down Expand Up @@ -41,6 +43,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
2B3A7F7F20629F1A008C4ED9 /* UIImage+StringStylizer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+StringStylizer.swift"; sourceTree = "<group>"; };
2BE7A28B20216C650039610C /* StringStylizer.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = StringStylizer.framework; sourceTree = BUILT_PRODUCTS_DIR; };
2BE7A29320216C650039610C /* StringStylizer tvOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "StringStylizer tvOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
6E3F29801CB2AB2600702DD3 /* StringStylizer.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = StringStylizer.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -118,6 +121,7 @@
6E3F299B1CB2AB3200702DD3 /* StringStylizerFontName.swift */,
6E3F299C1CB2AB3200702DD3 /* StringStylizerStatus.swift */,
6E3F299D1CB2AB3200702DD3 /* String+StringStylizer.swift */,
2B3A7F7F20629F1A008C4ED9 /* UIImage+StringStylizer.swift */,
6E3F299E1CB2AB3200702DD3 /* Operator.swift */,
6E3F29851CB2AB2600702DD3 /* Info.plist */,
);
Expand Down Expand Up @@ -316,6 +320,7 @@
2BE7A2A320216C980039610C /* StringStylizerFontName.swift in Sources */,
2BE7A2A420216C980039610C /* StringStylizerStatus.swift in Sources */,
2BE7A2A520216C980039610C /* String+StringStylizer.swift in Sources */,
2B3A7F8120629F24008C4ED9 /* UIImage+StringStylizer.swift in Sources */,
2BE7A2A620216C980039610C /* Operator.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -335,6 +340,7 @@
6E3F29A21CB2AB3200702DD3 /* String+StringStylizer.swift in Sources */,
6E3F29A31CB2AB3200702DD3 /* Operator.swift in Sources */,
6E3F299F1CB2AB3200702DD3 /* StringStylizer.swift in Sources */,
2B3A7F8020629F1A008C4ED9 /* UIImage+StringStylizer.swift in Sources */,
6E3F29A11CB2AB3200702DD3 /* StringStylizerStatus.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
23 changes: 21 additions & 2 deletions StringStylizer/Operator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import Foundation

import UIKit
/**
combine two NSAttributedString object
Expand Down Expand Up @@ -51,3 +50,23 @@ public func + (left: NSAttributedString, right: NSAttributedString) -> NSAttribu
public func += (left: inout NSAttributedString, right: NSAttributedString) {
left = left + right
}

/// combine two NSAttributedString and NSTextAttachment objects
///
/// - Parameters:
/// - left: NSAttributedString
/// - right: NSTextAttachment
/// - Returns: combinded NSAttributedString
public func + (left: NSAttributedString, right: NSTextAttachment) -> NSAttributedString {
return left + NSAttributedString(attachment: right)
}

/// combine two NSTextAttachment and NSAttributedString objects
///
/// - Parameters:
/// - left: NSTextAttachment
/// - right: NSAttributedString
/// - Returns: combinded NSAttributedString
public func + (left: NSTextAttachment, right: NSAttributedString) -> NSAttributedString {
return NSAttributedString(attachment: left) + right
}
1 change: 1 addition & 0 deletions StringStylizer/String+StringStylizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ public extension String {
return attributer.range(range)
}
}

35 changes: 35 additions & 0 deletions StringStylizer/UIImage+StringStylizer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// UIImage+StringStylizer.swift
// StringStylizer
//
// Created by kahayash on 2018/03/21.
// Copyright © 2018年 Kazuhiro Hayashi. All rights reserved.
//

import UIKit

public extension UIImage {

/// Instantiate NSTextAttachment from the uiimage object.
///
/// - Parameter size: attachmented image size
/// - Returns: NSTextAttachment
public func textAttachemnt(with size: CGSize) -> NSTextAttachment {
let textAttachemnt = NSTextAttachment()
textAttachemnt.image = self
textAttachemnt.bounds = CGRect(origin: .zero, size: size)
return textAttachemnt
}


/// Instantiate NSTextAttachment from the uiimage object.
///
/// - Parameters:
/// - width: attachmented image width
/// - height: attachmented image height
/// - Returns: NSTextAttachment
public func textAttachemnt(_ width: CGFloat, _ height: CGFloat) -> NSTextAttachment {
let size = CGSize(width: width, height: height)
return textAttachemnt(with: size)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
},
{
"idiom" : "ios-marketing",
"size" : "1024x1024",
"scale" : "1x"
}
],
"info" : {
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "18425372_1329221433799446_4389587629419729958_n.jpg",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
6 changes: 6 additions & 0 deletions iOS Sample/iOS Sample/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
3 changes: 1 addition & 2 deletions iOS Sample/iOS Sample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

centerLabel.attributedText = "aaa".stylize().color(.red).attr
centerLabel.attributedText = "aaa".stylize().color(.red).attr + #imageLiteral(resourceName: "Attachment").textAttachemnt(17, 17)
}

override func didReceiveMemoryWarning() {
Expand Down

0 comments on commit a52a1f0

Please sign in to comment.