diff --git a/TelegramUI/AuthorizationSequenceSplashController.swift b/TelegramUI/AuthorizationSequenceSplashController.swift index 71a82f7abd..be85c08cf4 100644 --- a/TelegramUI/AuthorizationSequenceSplashController.swift +++ b/TelegramUI/AuthorizationSequenceSplashController.swift @@ -21,7 +21,7 @@ final class AuthorizationSequenceSplashController: ViewController { var nextPressed: ((PresentationStrings?) -> Void)? - private let suggestedLocalization = Promise(nil) + private let suggestedLocalization = Promise() private let activateLocalizationDisposable = MetaDisposable() init(postbox: Postbox, network: Network, theme: AuthorizationTheme) { @@ -29,7 +29,8 @@ final class AuthorizationSequenceSplashController: ViewController { self.network = network self.theme = theme - self.suggestedLocalization.set(currentlySuggestedLocalization(network: network, extractKeys: ["Login.ContinueWithLocalization"])) + self.suggestedLocalization.set(.single(nil) + |> then(currentlySuggestedLocalization(network: network, extractKeys: ["Login.ContinueWithLocalization"]))) let suggestedLocalization = self.suggestedLocalization let localizationSignal = SSignal(generator: { subscriber in diff --git a/TelegramUI/InstantPageControllerNode.swift b/TelegramUI/InstantPageControllerNode.swift index 5c719bcb8e..641c62fd80 100644 --- a/TelegramUI/InstantPageControllerNode.swift +++ b/TelegramUI/InstantPageControllerNode.swift @@ -893,11 +893,16 @@ final class InstantPageControllerNode: ASDisplayNode, UIScrollViewDelegate { } } - private func findAnchorItem(_ anchor: String, items: [InstantPageItem]) -> InstantPageAnchorItem? { + private func findAnchorItem(_ anchor: String, items: [InstantPageItem]) -> (InstantPageItem, CGFloat)? { for item in items { if let item = item as? InstantPageAnchorItem, item.anchor == anchor { - return item - } else if let item = item as? InstantPageDetailsItem { + return (item, 0.0) + } else if let item = item as? InstantPageTextItem { + if let lineIndex = item.anchors[anchor] { + return (item, item.lines[lineIndex].frame.minY - 10.0) + } + } + else if let item = item as? InstantPageDetailsItem { if let anchorItem = findAnchorItem(anchor, items: item.items) { return anchorItem } @@ -910,12 +915,18 @@ final class InstantPageControllerNode: ASDisplayNode, UIScrollViewDelegate { guard let items = self.currentLayout?.items else { return } - - if let webPage = self.webPage, url.webpageId == webPage.id, let anchorRange = url.url.range(of: "#") { - let anchor = url.url[anchorRange.upperBound...] + + var baseUrl = url.url + var anchor: String? + if let anchorRange = url.url.range(of: "#") { + anchor = String(baseUrl[anchorRange.upperBound...]) + baseUrl = String(baseUrl[..() + var anchors: [String: Int] = [:] for line in lines { if line.isRTL { rtlLineIndices.insert(index) } + for anchor in line.anchorItems { + anchors[anchor.name] = index + } index += 1 } self.rtlLineIndices = rtlLineIndices + self.anchors = anchors } func drawInTile(context: CGContext) { @@ -410,6 +422,15 @@ func attributedStringForRichText(_ text: RichText, styleStack: InstantPageTextSt let delegate = CTRunDelegateCreate(&callbacks, extentBuffer) let attrDictionaryDelegate = [(kCTRunDelegateAttributeName as NSAttributedStringKey): (delegate as Any), NSAttributedStringKey(rawValue: InstantPageMediaIdAttribute): id.id] return NSAttributedString(string: " ", attributes: attrDictionaryDelegate) + case let .anchor(text, name): + styleStack.push(.anchor(name)) + var text = text + if case .empty = text { + text = .plain("\u{200b}") + } + let result = attributedStringForRichText(text, styleStack: styleStack, url: url) + styleStack.pop() + return result } } @@ -497,6 +518,7 @@ func layoutTextItemWithString(_ string: NSAttributedString, boundingWidth: CGFlo var strikethroughItems: [InstantPageTextStrikethroughItem] = [] var markedItems: [InstantPageTextMarkedItem] = [] + var anchorItems: [InstantPageTextAnchorItem] = [] string.enumerateAttributes(in: lineRange, options: []) { attributes, range, _ in if let _ = attributes[NSAttributedStringKey.strikethroughStyle] { @@ -507,6 +529,8 @@ func layoutTextItemWithString(_ string: NSAttributedString, boundingWidth: CGFlo let lowerX = floor(CTLineGetOffsetForStringIndex(line, range.location, nil)) let upperX = ceil(CTLineGetOffsetForStringIndex(line, range.location + range.length, nil)) markedItems.append(InstantPageTextMarkedItem(frame: CGRect(x: currentLineOrigin.x + lowerX, y: currentLineOrigin.y, width: upperX - lowerX, height: fontLineHeight), color: item)) + } else if let item = attributes[NSAttributedStringKey.init(rawValue: InstantPageAnchorAttribute)] as? String { + anchorItems.append(InstantPageTextAnchorItem(name: item)) } } @@ -556,7 +580,7 @@ func layoutTextItemWithString(_ string: NSAttributedString, boundingWidth: CGFlo } let height = !fontLineHeight.isZero ? fontLineHeight : maxImageHeight - let textLine = InstantPageTextLine(line: line, range: lineRange, frame: CGRect(x: currentLineOrigin.x, y: currentLineOrigin.y, width: lineWidth, height: height), strikethroughItems: strikethroughItems, markedItems: markedItems, imageItems: lineImageItems, isRTL: isRTL) + let textLine = InstantPageTextLine(line: line, range: lineRange, frame: CGRect(x: currentLineOrigin.x, y: currentLineOrigin.y, width: lineWidth, height: height), strikethroughItems: strikethroughItems, markedItems: markedItems, imageItems: lineImageItems, anchorItems: anchorItems, isRTL: isRTL) lines.append(textLine) imageItems.append(contentsOf: lineImageItems) diff --git a/TelegramUI/InstantPageTextStyleStack.swift b/TelegramUI/InstantPageTextStyleStack.swift index 9e603a920b..05a0764c47 100644 --- a/TelegramUI/InstantPageTextStyleStack.swift +++ b/TelegramUI/InstantPageTextStyleStack.swift @@ -16,11 +16,13 @@ enum InstantPageTextStyle { case superscript case markerColor(UIColor) case marker + case anchor(String) } let InstantPageLineSpacingFactorAttribute = "LineSpacingFactorAttribute" let InstantPageMarkerColorAttribute = "MarkerColorAttribute" let InstantPageMediaIdAttribute = "MediaIdAttribute" +let InstantPageAnchorAttribute = "AnchorAttribute" final class InstantPageTextStyleStack { private var items: [InstantPageTextStyle] = [] @@ -48,6 +50,7 @@ final class InstantPageTextStyleStack { var baselineOffset: CGFloat? var markerColor: UIColor? var marker: Bool? + var anchor: String? for item in self.items.reversed() { switch item { @@ -104,6 +107,10 @@ final class InstantPageTextStyleStack { if marker == nil { marker = true } + case let .anchor(name): + if anchor == nil { + anchor = name + } } } @@ -177,6 +184,10 @@ final class InstantPageTextStyleStack { attributes[NSAttributedStringKey(rawValue: InstantPageMarkerColorAttribute)] = markerColor } + if let anchor = anchor { + attributes[NSAttributedStringKey(rawValue: InstantPageAnchorAttribute)] = anchor + } + return attributes } } diff --git a/TelegramUI/LanguageSuggestionController.swift b/TelegramUI/LanguageSuggestionController.swift index 24b5cc4a3b..5aad335071 100644 --- a/TelegramUI/LanguageSuggestionController.swift +++ b/TelegramUI/LanguageSuggestionController.swift @@ -288,6 +288,9 @@ private final class LanguageSuggestionAlertContentNode: AlertContentNode { } override func updateLayout(size: CGSize, transition: ContainedViewLayoutTransition) -> CGSize { + var size = size + size.width = min(size.width , 270.0) + self.validLayout = size var origin: CGPoint = CGPoint(x: 0.0, y: 17.0)