Skip to content

Commit

Permalink
Added image inside attributes as example.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandru Tudose committed Feb 26, 2019
1 parent e6b4ddd commit ca533c4
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 10 deletions.
12 changes: 10 additions & 2 deletions TTSegmentedControl.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
objects = {

/* Begin PBXBuildFile section */
239299BE222547EE00263573 /* TTSegmentedControl.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 81535A791F88BC7300398A9A /* TTSegmentedControl.framework */; };
8193E5D41F8A70A900E1604E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8193E5D31F8A70A900E1604E /* AppDelegate.swift */; };
8193E5D61F8A70A900E1604E /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8193E5D51F8A70A900E1604E /* ViewController.swift */; };
8193E5D91F8A70A900E1604E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8193E5D71F8A70A900E1604E /* Main.storyboard */; };
8193E5DB1F8A70A900E1604E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8193E5DA1F8A70A900E1604E /* Assets.xcassets */; };
8193E5DE1F8A70A900E1604E /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8193E5DC1F8A70A900E1604E /* LaunchScreen.storyboard */; };
8193E5E71F8A725700E1604E /* TTSegmentedControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81535A881F88BD2A00398A9A /* TTSegmentedControl.swift */; };
8193E5E81F8A725800E1604E /* TTSegmentedControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81535A881F88BD2A00398A9A /* TTSegmentedControl.swift */; };
8193E5EA1F8A725F00E1604E /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 81535A891F88BD2A00398A9A /* Info.plist */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -43,18 +43,27 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
239299BE222547EE00263573 /* TTSegmentedControl.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
239299BD222547EE00263573 /* Frameworks */ = {
isa = PBXGroup;
children = (
);
name = Frameworks;
sourceTree = "<group>";
};
81535A6F1F88BC7300398A9A = {
isa = PBXGroup;
children = (
81535A7B1F88BC7300398A9A /* TTSegmentedControl */,
8193E5D21F8A70A900E1604E /* TTSegmentedControlSample */,
81535A7A1F88BC7300398A9A /* Products */,
239299BD222547EE00263573 /* Frameworks */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -212,7 +221,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
8193E5E81F8A725800E1604E /* TTSegmentedControl.swift in Sources */,
8193E5D61F8A70A900E1604E /* ViewController.swift in Sources */,
8193E5D41F8A70A900E1604E /* AppDelegate.swift in Sources */,
);
Expand Down
4 changes: 2 additions & 2 deletions TTSegmentedControl/TTSegmentedControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ open class TTSegmentedControl: UIView {

open var itemTitles: [String] = ["Item1", "Item2", "Item3"]

var attributedDefaultTitles: [NSAttributedString]!
var attributedSelectedTitles: [NSAttributedString]!
open var attributedDefaultTitles: [NSAttributedString]!
open var attributedSelectedTitles: [NSAttributedString]!
/*
Gets called when an item is selected
*/
Expand Down
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
6 changes: 6 additions & 0 deletions TTSegmentedControlSample/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "phone.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 6 additions & 5 deletions TTSegmentedControlSample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13196" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13173"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -17,9 +18,9 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="pAv-Sl-fTL" customClass="TTSegmentedControl" customModule="TTSegmentedControlSample" customModuleProvider="target">
<rect key="frame" x="78" y="87" width="240" height="60"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="pAv-Sl-fTL" customClass="TTSegmentedControl" customModule="TTSegmentedControl">
<rect key="frame" x="21" y="87" width="333" height="60"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="cornerRadius">
Expand Down
35 changes: 34 additions & 1 deletion TTSegmentedControlSample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import UIKit

import TTSegmentedControl

class ViewController: UIViewController {

Expand All @@ -16,12 +16,45 @@ class ViewController: UIViewController {
super.viewDidLoad()
segmentedControl.selectItemAt(index: 2, animated: false)

useImageInsideAttributes(image: UIImage(named: "phone")!, atIndex: 2)
addImageInsideAttributes(image: UIImage(named: "phone")!, atIndex: 1)
}

func addImageInsideAttributes(image: UIImage, atIndex: Int) {
// ask segmented control to initialize all elements internally
segmentedControl.layoutSubviews()

let imageAttachment = NSTextAttachment()
imageAttachment.image = image
imageAttachment.bounds = CGRect(x: 0, y: -5, width: 20, height: 20)

let attributes = segmentedControl.attributedDefaultTitles.first?.mutableCopy() as! NSMutableAttributedString
attributes.append(NSAttributedString(attachment: imageAttachment))

let selectedAttributes = segmentedControl.attributedSelectedTitles.first?.mutableCopy() as! NSMutableAttributedString
selectedAttributes.append(NSAttributedString(attachment: imageAttachment))

segmentedControl.changeAttributedTitle(attributes, selectedTile: selectedAttributes, atIndex: atIndex)
}

func useImageInsideAttributes(image: UIImage, atIndex: Int) {
// ask segmented control to initialize all elements internally
segmentedControl.layoutSubviews()

let imageAttachment = NSTextAttachment()
imageAttachment.image = image
imageAttachment.bounds = CGRect(x: 0, y: -5, width: 20, height: 20)

let attributes = NSAttributedString(attachment: imageAttachment)

segmentedControl.changeAttributedTitle(attributes, selectedTile: attributes, atIndex: atIndex)
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// segmentedControl.noItemSelected = true
segmentedControl.hasBounceAnimation = true

}
@IBAction func buttonAction(_ sender: Any) {
segmentedControl.changeTitle("asdfasdfsdfsdffasdfsdf", atIndex: 2)
Expand Down

0 comments on commit ca533c4

Please sign in to comment.